1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/9.2/sys/kern/kern_descrip.c 252829 2013-07-05 19:38:57Z trociny $");
39
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_ddb.h"
43 #include "opt_ktrace.h"
44 #include "opt_procdesc.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48
49 #include <sys/capability.h>
50 #include <sys/conf.h>
51 #include <sys/domain.h>
52 #include <sys/fcntl.h>
53 #include <sys/file.h>
54 #include <sys/filedesc.h>
55 #include <sys/filio.h>
56 #include <sys/jail.h>
57 #include <sys/kernel.h>
58 #include <sys/ksem.h>
59 #include <sys/limits.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/mman.h>
63 #include <sys/mount.h>
64 #include <sys/mqueue.h>
65 #include <sys/mutex.h>
66 #include <sys/namei.h>
67 #include <sys/selinfo.h>
68 #include <sys/pipe.h>
69 #include <sys/priv.h>
70 #include <sys/proc.h>
71 #include <sys/procdesc.h>
72 #include <sys/protosw.h>
73 #include <sys/racct.h>
74 #include <sys/resourcevar.h>
75 #include <sys/sbuf.h>
76 #include <sys/signalvar.h>
77 #include <sys/socketvar.h>
78 #include <sys/stat.h>
79 #include <sys/sx.h>
80 #include <sys/syscallsubr.h>
81 #include <sys/sysctl.h>
82 #include <sys/sysproto.h>
83 #include <sys/tty.h>
84 #include <sys/unistd.h>
85 #include <sys/un.h>
86 #include <sys/unpcb.h>
87 #include <sys/user.h>
88 #include <sys/vnode.h>
89 #ifdef KTRACE
90 #include <sys/ktrace.h>
91 #endif
92
93 #include <net/vnet.h>
94
95 #include <netinet/in.h>
96 #include <netinet/in_pcb.h>
97
98 #include <security/audit/audit.h>
99
100 #include <vm/uma.h>
101 #include <vm/vm.h>
102
103 #include <ddb/ddb.h>
104
105 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
106 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
107 "file desc to leader structures");
108 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
109
110 MALLOC_DECLARE(M_FADVISE);
111
112 static uma_zone_t file_zone;
113
114 void (*ksem_info)(struct ksem *ks, char *path, size_t size, uint32_t *value);
115
116 /* Flags for do_dup() */
117 #define DUP_FIXED 0x1 /* Force fixed allocation */
118 #define DUP_FCNTL 0x2 /* fcntl()-style errors */
119 #define DUP_CLOEXEC 0x4 /* Atomically set FD_CLOEXEC. */
120
121 static int do_dup(struct thread *td, int flags, int old, int new,
122 register_t *retval);
123 static int fd_first_free(struct filedesc *, int, int);
124 static int fd_last_used(struct filedesc *, int, int);
125 static void fdgrowtable(struct filedesc *, int);
126 static void fdunused(struct filedesc *fdp, int fd);
127 static void fdused(struct filedesc *fdp, int fd);
128 static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
129 static int fill_socket_info(struct socket *so, struct kinfo_file *kif);
130 static int fill_pts_info(struct tty *tp, struct kinfo_file *kif);
131 static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
132 static int fill_procdesc_info(struct procdesc *pdp,
133 struct kinfo_file *kif);
134 static int fill_sem_info(struct file *fp, struct kinfo_file *kif);
135 static int fill_shm_info(struct file *fp, struct kinfo_file *kif);
136
137 /*
138 * A process is initially started out with NDFILE descriptors stored within
139 * this structure, selected to be enough for typical applications based on
140 * the historical limit of 20 open files (and the usage of descriptors by
141 * shells). If these descriptors are exhausted, a larger descriptor table
142 * may be allocated, up to a process' resource limit; the internal arrays
143 * are then unused.
144 */
145 #define NDFILE 20
146 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
147 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
148 #define NDSLOT(x) ((x) / NDENTRIES)
149 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
150 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
151
152 /*
153 * Storage required per open file descriptor.
154 */
155 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
156
157 /*
158 * Storage to hold unused ofiles that need to be reclaimed.
159 */
160 struct freetable {
161 struct file **ft_table;
162 SLIST_ENTRY(freetable) ft_next;
163 };
164
165 /*
166 * Basic allocation of descriptors:
167 * one of the above, plus arrays for NDFILE descriptors.
168 */
169 struct filedesc0 {
170 struct filedesc fd_fd;
171 /*
172 * ofiles which need to be reclaimed on free.
173 */
174 SLIST_HEAD(,freetable) fd_free;
175 /*
176 * These arrays are used when the number of open files is
177 * <= NDFILE, and are then pointed to by the pointers above.
178 */
179 struct file *fd_dfiles[NDFILE];
180 char fd_dfileflags[NDFILE];
181 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
182 };
183
184 /*
185 * Descriptor management.
186 */
187 volatile int openfiles; /* actual number of open files */
188 struct mtx sigio_lock; /* mtx to protect pointers to sigio */
189 void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
190
191 /* A mutex to protect the association between a proc and filedesc. */
192 static struct mtx fdesc_mtx;
193
194 /*
195 * Find the first zero bit in the given bitmap, starting at low and not
196 * exceeding size - 1.
197 */
198 static int
199 fd_first_free(struct filedesc *fdp, int low, int size)
200 {
201 NDSLOTTYPE *map = fdp->fd_map;
202 NDSLOTTYPE mask;
203 int off, maxoff;
204
205 if (low >= size)
206 return (low);
207
208 off = NDSLOT(low);
209 if (low % NDENTRIES) {
210 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
211 if ((mask &= ~map[off]) != 0UL)
212 return (off * NDENTRIES + ffsl(mask) - 1);
213 ++off;
214 }
215 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
216 if (map[off] != ~0UL)
217 return (off * NDENTRIES + ffsl(~map[off]) - 1);
218 return (size);
219 }
220
221 /*
222 * Find the highest non-zero bit in the given bitmap, starting at low and
223 * not exceeding size - 1.
224 */
225 static int
226 fd_last_used(struct filedesc *fdp, int low, int size)
227 {
228 NDSLOTTYPE *map = fdp->fd_map;
229 NDSLOTTYPE mask;
230 int off, minoff;
231
232 if (low >= size)
233 return (-1);
234
235 off = NDSLOT(size);
236 if (size % NDENTRIES) {
237 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
238 if ((mask &= map[off]) != 0)
239 return (off * NDENTRIES + flsl(mask) - 1);
240 --off;
241 }
242 for (minoff = NDSLOT(low); off >= minoff; --off)
243 if (map[off] != 0)
244 return (off * NDENTRIES + flsl(map[off]) - 1);
245 return (low - 1);
246 }
247
248 static int
249 fdisused(struct filedesc *fdp, int fd)
250 {
251 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
252 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
253 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
254 }
255
256 /*
257 * Mark a file descriptor as used.
258 */
259 static void
260 fdused(struct filedesc *fdp, int fd)
261 {
262
263 FILEDESC_XLOCK_ASSERT(fdp);
264 KASSERT(!fdisused(fdp, fd),
265 ("fd already used"));
266
267 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
268 if (fd > fdp->fd_lastfile)
269 fdp->fd_lastfile = fd;
270 if (fd == fdp->fd_freefile)
271 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
272 }
273
274 /*
275 * Mark a file descriptor as unused.
276 */
277 static void
278 fdunused(struct filedesc *fdp, int fd)
279 {
280
281 FILEDESC_XLOCK_ASSERT(fdp);
282 KASSERT(fdisused(fdp, fd),
283 ("fd is already unused"));
284 KASSERT(fdp->fd_ofiles[fd] == NULL,
285 ("fd is still in use"));
286
287 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
288 if (fd < fdp->fd_freefile)
289 fdp->fd_freefile = fd;
290 if (fd == fdp->fd_lastfile)
291 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
292 }
293
294 /*
295 * System calls on descriptors.
296 */
297 #ifndef _SYS_SYSPROTO_H_
298 struct getdtablesize_args {
299 int dummy;
300 };
301 #endif
302 /* ARGSUSED */
303 int
304 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
305 {
306 struct proc *p = td->td_proc;
307 uint64_t lim;
308
309 PROC_LOCK(p);
310 td->td_retval[0] =
311 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
312 lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
313 PROC_UNLOCK(p);
314 if (lim < td->td_retval[0])
315 td->td_retval[0] = lim;
316 return (0);
317 }
318
319 /*
320 * Duplicate a file descriptor to a particular value.
321 *
322 * Note: keep in mind that a potential race condition exists when closing
323 * descriptors from a shared descriptor table (via rfork).
324 */
325 #ifndef _SYS_SYSPROTO_H_
326 struct dup2_args {
327 u_int from;
328 u_int to;
329 };
330 #endif
331 /* ARGSUSED */
332 int
333 sys_dup2(struct thread *td, struct dup2_args *uap)
334 {
335
336 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
337 td->td_retval));
338 }
339
340 /*
341 * Duplicate a file descriptor.
342 */
343 #ifndef _SYS_SYSPROTO_H_
344 struct dup_args {
345 u_int fd;
346 };
347 #endif
348 /* ARGSUSED */
349 int
350 sys_dup(struct thread *td, struct dup_args *uap)
351 {
352
353 return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
354 }
355
356 /*
357 * The file control system call.
358 */
359 #ifndef _SYS_SYSPROTO_H_
360 struct fcntl_args {
361 int fd;
362 int cmd;
363 long arg;
364 };
365 #endif
366 /* ARGSUSED */
367 int
368 sys_fcntl(struct thread *td, struct fcntl_args *uap)
369 {
370 struct flock fl;
371 struct __oflock ofl;
372 intptr_t arg;
373 int error;
374 int cmd;
375
376 error = 0;
377 cmd = uap->cmd;
378 switch (uap->cmd) {
379 case F_OGETLK:
380 case F_OSETLK:
381 case F_OSETLKW:
382 /*
383 * Convert old flock structure to new.
384 */
385 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
386 fl.l_start = ofl.l_start;
387 fl.l_len = ofl.l_len;
388 fl.l_pid = ofl.l_pid;
389 fl.l_type = ofl.l_type;
390 fl.l_whence = ofl.l_whence;
391 fl.l_sysid = 0;
392
393 switch (uap->cmd) {
394 case F_OGETLK:
395 cmd = F_GETLK;
396 break;
397 case F_OSETLK:
398 cmd = F_SETLK;
399 break;
400 case F_OSETLKW:
401 cmd = F_SETLKW;
402 break;
403 }
404 arg = (intptr_t)&fl;
405 break;
406 case F_GETLK:
407 case F_SETLK:
408 case F_SETLKW:
409 case F_SETLK_REMOTE:
410 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
411 arg = (intptr_t)&fl;
412 break;
413 default:
414 arg = uap->arg;
415 break;
416 }
417 if (error)
418 return (error);
419 error = kern_fcntl(td, uap->fd, cmd, arg);
420 if (error)
421 return (error);
422 if (uap->cmd == F_OGETLK) {
423 ofl.l_start = fl.l_start;
424 ofl.l_len = fl.l_len;
425 ofl.l_pid = fl.l_pid;
426 ofl.l_type = fl.l_type;
427 ofl.l_whence = fl.l_whence;
428 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
429 } else if (uap->cmd == F_GETLK) {
430 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
431 }
432 return (error);
433 }
434
435 static inline struct file *
436 fdtofp(int fd, struct filedesc *fdp)
437 {
438 struct file *fp;
439
440 FILEDESC_LOCK_ASSERT(fdp);
441 if ((unsigned)fd >= fdp->fd_nfiles ||
442 (fp = fdp->fd_ofiles[fd]) == NULL)
443 return (NULL);
444 return (fp);
445 }
446
447 static inline int
448 fdunwrap(int fd, cap_rights_t rights, struct filedesc *fdp, struct file **fpp)
449 {
450
451 *fpp = fdtofp(fd, fdp);
452 if (*fpp == NULL)
453 return (EBADF);
454
455 #ifdef CAPABILITIES
456 if ((*fpp)->f_type == DTYPE_CAPABILITY) {
457 int err = cap_funwrap(*fpp, rights, fpp);
458 if (err != 0) {
459 *fpp = NULL;
460 return (err);
461 }
462 }
463 #endif /* CAPABILITIES */
464 return (0);
465 }
466
467 int
468 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
469 {
470 struct filedesc *fdp;
471 struct flock *flp;
472 struct file *fp;
473 struct proc *p;
474 char *pop;
475 struct vnode *vp;
476 int error, flg, tmp;
477 int vfslocked;
478 u_int old, new;
479 uint64_t bsize;
480 off_t foffset;
481
482 vfslocked = 0;
483 error = 0;
484 flg = F_POSIX;
485 p = td->td_proc;
486 fdp = p->p_fd;
487
488 switch (cmd) {
489 case F_DUPFD:
490 tmp = arg;
491 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
492 break;
493
494 case F_DUPFD_CLOEXEC:
495 tmp = arg;
496 error = do_dup(td, DUP_FCNTL | DUP_CLOEXEC, fd, tmp,
497 td->td_retval);
498 break;
499
500 case F_DUP2FD:
501 tmp = arg;
502 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
503 break;
504
505 case F_DUP2FD_CLOEXEC:
506 tmp = arg;
507 error = do_dup(td, DUP_FIXED | DUP_CLOEXEC, fd, tmp,
508 td->td_retval);
509 break;
510
511 case F_GETFD:
512 FILEDESC_SLOCK(fdp);
513 if ((fp = fdtofp(fd, fdp)) == NULL) {
514 FILEDESC_SUNLOCK(fdp);
515 error = EBADF;
516 break;
517 }
518 pop = &fdp->fd_ofileflags[fd];
519 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
520 FILEDESC_SUNLOCK(fdp);
521 break;
522
523 case F_SETFD:
524 FILEDESC_XLOCK(fdp);
525 if ((fp = fdtofp(fd, fdp)) == NULL) {
526 FILEDESC_XUNLOCK(fdp);
527 error = EBADF;
528 break;
529 }
530 pop = &fdp->fd_ofileflags[fd];
531 *pop = (*pop &~ UF_EXCLOSE) |
532 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
533 FILEDESC_XUNLOCK(fdp);
534 break;
535
536 case F_GETFL:
537 FILEDESC_SLOCK(fdp);
538 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
539 if (error != 0) {
540 FILEDESC_SUNLOCK(fdp);
541 break;
542 }
543 td->td_retval[0] = OFLAGS(fp->f_flag);
544 FILEDESC_SUNLOCK(fdp);
545 break;
546
547 case F_SETFL:
548 FILEDESC_SLOCK(fdp);
549 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
550 if (error != 0) {
551 FILEDESC_SUNLOCK(fdp);
552 break;
553 }
554 fhold(fp);
555 FILEDESC_SUNLOCK(fdp);
556 do {
557 tmp = flg = fp->f_flag;
558 tmp &= ~FCNTLFLAGS;
559 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
560 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
561 tmp = fp->f_flag & FNONBLOCK;
562 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
563 if (error) {
564 fdrop(fp, td);
565 break;
566 }
567 tmp = fp->f_flag & FASYNC;
568 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
569 if (error == 0) {
570 fdrop(fp, td);
571 break;
572 }
573 atomic_clear_int(&fp->f_flag, FNONBLOCK);
574 tmp = 0;
575 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
576 fdrop(fp, td);
577 break;
578
579 case F_GETOWN:
580 FILEDESC_SLOCK(fdp);
581 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
582 if (error != 0) {
583 FILEDESC_SUNLOCK(fdp);
584 break;
585 }
586 fhold(fp);
587 FILEDESC_SUNLOCK(fdp);
588 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
589 if (error == 0)
590 td->td_retval[0] = tmp;
591 fdrop(fp, td);
592 break;
593
594 case F_SETOWN:
595 FILEDESC_SLOCK(fdp);
596 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
597 if (error != 0) {
598 FILEDESC_SUNLOCK(fdp);
599 break;
600 }
601 fhold(fp);
602 FILEDESC_SUNLOCK(fdp);
603 tmp = arg;
604 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
605 fdrop(fp, td);
606 break;
607
608 case F_SETLK_REMOTE:
609 error = priv_check(td, PRIV_NFS_LOCKD);
610 if (error)
611 return (error);
612 flg = F_REMOTE;
613 goto do_setlk;
614
615 case F_SETLKW:
616 flg |= F_WAIT;
617 /* FALLTHROUGH F_SETLK */
618
619 case F_SETLK:
620 do_setlk:
621 FILEDESC_SLOCK(fdp);
622 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
623 if (error != 0) {
624 FILEDESC_SUNLOCK(fdp);
625 break;
626 }
627 if (fp->f_type != DTYPE_VNODE) {
628 FILEDESC_SUNLOCK(fdp);
629 error = EBADF;
630 break;
631 }
632 flp = (struct flock *)arg;
633 if (flp->l_whence == SEEK_CUR) {
634 foffset = foffset_get(fp);
635 if (foffset < 0 ||
636 (flp->l_start > 0 &&
637 foffset > OFF_MAX - flp->l_start)) {
638 FILEDESC_SUNLOCK(fdp);
639 error = EOVERFLOW;
640 break;
641 }
642 flp->l_start += foffset;
643 }
644
645 /*
646 * VOP_ADVLOCK() may block.
647 */
648 fhold(fp);
649 FILEDESC_SUNLOCK(fdp);
650 vp = fp->f_vnode;
651 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
652 switch (flp->l_type) {
653 case F_RDLCK:
654 if ((fp->f_flag & FREAD) == 0) {
655 error = EBADF;
656 break;
657 }
658 PROC_LOCK(p->p_leader);
659 p->p_leader->p_flag |= P_ADVLOCK;
660 PROC_UNLOCK(p->p_leader);
661 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
662 flp, flg);
663 break;
664 case F_WRLCK:
665 if ((fp->f_flag & FWRITE) == 0) {
666 error = EBADF;
667 break;
668 }
669 PROC_LOCK(p->p_leader);
670 p->p_leader->p_flag |= P_ADVLOCK;
671 PROC_UNLOCK(p->p_leader);
672 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
673 flp, flg);
674 break;
675 case F_UNLCK:
676 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
677 flp, flg);
678 break;
679 case F_UNLCKSYS:
680 /*
681 * Temporary api for testing remote lock
682 * infrastructure.
683 */
684 if (flg != F_REMOTE) {
685 error = EINVAL;
686 break;
687 }
688 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
689 F_UNLCKSYS, flp, flg);
690 break;
691 default:
692 error = EINVAL;
693 break;
694 }
695 VFS_UNLOCK_GIANT(vfslocked);
696 vfslocked = 0;
697 /* Check for race with close */
698 FILEDESC_SLOCK(fdp);
699 if ((unsigned) fd >= fdp->fd_nfiles ||
700 fp != fdp->fd_ofiles[fd]) {
701 FILEDESC_SUNLOCK(fdp);
702 flp->l_whence = SEEK_SET;
703 flp->l_start = 0;
704 flp->l_len = 0;
705 flp->l_type = F_UNLCK;
706 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
707 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
708 F_UNLCK, flp, F_POSIX);
709 VFS_UNLOCK_GIANT(vfslocked);
710 vfslocked = 0;
711 } else
712 FILEDESC_SUNLOCK(fdp);
713 fdrop(fp, td);
714 break;
715
716 case F_GETLK:
717 FILEDESC_SLOCK(fdp);
718 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
719 if (error != 0) {
720 FILEDESC_SUNLOCK(fdp);
721 break;
722 }
723 if (fp->f_type != DTYPE_VNODE) {
724 FILEDESC_SUNLOCK(fdp);
725 error = EBADF;
726 break;
727 }
728 flp = (struct flock *)arg;
729 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
730 flp->l_type != F_UNLCK) {
731 FILEDESC_SUNLOCK(fdp);
732 error = EINVAL;
733 break;
734 }
735 if (flp->l_whence == SEEK_CUR) {
736 foffset = foffset_get(fp);
737 if ((flp->l_start > 0 &&
738 foffset > OFF_MAX - flp->l_start) ||
739 (flp->l_start < 0 &&
740 foffset < OFF_MIN - flp->l_start)) {
741 FILEDESC_SUNLOCK(fdp);
742 error = EOVERFLOW;
743 break;
744 }
745 flp->l_start += foffset;
746 }
747 /*
748 * VOP_ADVLOCK() may block.
749 */
750 fhold(fp);
751 FILEDESC_SUNLOCK(fdp);
752 vp = fp->f_vnode;
753 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
754 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
755 F_POSIX);
756 VFS_UNLOCK_GIANT(vfslocked);
757 vfslocked = 0;
758 fdrop(fp, td);
759 break;
760
761 case F_RDAHEAD:
762 arg = arg ? 128 * 1024: 0;
763 /* FALLTHROUGH */
764 case F_READAHEAD:
765 FILEDESC_SLOCK(fdp);
766 if ((fp = fdtofp(fd, fdp)) == NULL) {
767 FILEDESC_SUNLOCK(fdp);
768 error = EBADF;
769 break;
770 }
771 if (fp->f_type != DTYPE_VNODE) {
772 FILEDESC_SUNLOCK(fdp);
773 error = EBADF;
774 break;
775 }
776 fhold(fp);
777 FILEDESC_SUNLOCK(fdp);
778 if (arg != 0) {
779 vp = fp->f_vnode;
780 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
781 error = vn_lock(vp, LK_SHARED);
782 if (error != 0)
783 goto readahead_vnlock_fail;
784 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
785 VOP_UNLOCK(vp, 0);
786 fp->f_seqcount = (arg + bsize - 1) / bsize;
787 do {
788 new = old = fp->f_flag;
789 new |= FRDAHEAD;
790 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
791 readahead_vnlock_fail:
792 VFS_UNLOCK_GIANT(vfslocked);
793 vfslocked = 0;
794 } else {
795 do {
796 new = old = fp->f_flag;
797 new &= ~FRDAHEAD;
798 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
799 }
800 fdrop(fp, td);
801 break;
802
803 default:
804 error = EINVAL;
805 break;
806 }
807 VFS_UNLOCK_GIANT(vfslocked);
808 return (error);
809 }
810
811 /*
812 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
813 */
814 static int
815 do_dup(struct thread *td, int flags, int old, int new,
816 register_t *retval)
817 {
818 struct filedesc *fdp;
819 struct proc *p;
820 struct file *fp;
821 struct file *delfp;
822 int error, holdleaders, maxfd;
823
824 p = td->td_proc;
825 fdp = p->p_fd;
826
827 /*
828 * Verify we have a valid descriptor to dup from and possibly to
829 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
830 * return EINVAL when the new descriptor is out of bounds.
831 */
832 if (old < 0)
833 return (EBADF);
834 if (new < 0)
835 return (flags & DUP_FCNTL ? EINVAL : EBADF);
836 PROC_LOCK(p);
837 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
838 PROC_UNLOCK(p);
839 if (new >= maxfd)
840 return (flags & DUP_FCNTL ? EINVAL : EBADF);
841
842 FILEDESC_XLOCK(fdp);
843 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
844 FILEDESC_XUNLOCK(fdp);
845 return (EBADF);
846 }
847 if (flags & DUP_FIXED && old == new) {
848 *retval = new;
849 if (flags & DUP_CLOEXEC)
850 fdp->fd_ofileflags[new] |= UF_EXCLOSE;
851 FILEDESC_XUNLOCK(fdp);
852 return (0);
853 }
854 fp = fdp->fd_ofiles[old];
855 fhold(fp);
856
857 /*
858 * If the caller specified a file descriptor, make sure the file
859 * table is large enough to hold it, and grab it. Otherwise, just
860 * allocate a new descriptor the usual way. Since the filedesc
861 * lock may be temporarily dropped in the process, we have to look
862 * out for a race.
863 */
864 if (flags & DUP_FIXED) {
865 if (new >= fdp->fd_nfiles) {
866 /*
867 * The resource limits are here instead of e.g.
868 * fdalloc(), because the file descriptor table may be
869 * shared between processes, so we can't really use
870 * racct_add()/racct_sub(). Instead of counting the
871 * number of actually allocated descriptors, just put
872 * the limit on the size of the file descriptor table.
873 */
874 #ifdef RACCT
875 PROC_LOCK(p);
876 error = racct_set(p, RACCT_NOFILE, new + 1);
877 PROC_UNLOCK(p);
878 if (error != 0) {
879 FILEDESC_XUNLOCK(fdp);
880 fdrop(fp, td);
881 return (EMFILE);
882 }
883 #endif
884 fdgrowtable(fdp, new + 1);
885 }
886 if (fdp->fd_ofiles[new] == NULL)
887 fdused(fdp, new);
888 } else {
889 if ((error = fdalloc(td, new, &new)) != 0) {
890 FILEDESC_XUNLOCK(fdp);
891 fdrop(fp, td);
892 return (error);
893 }
894 }
895
896 /*
897 * If the old file changed out from under us then treat it as a
898 * bad file descriptor. Userland should do its own locking to
899 * avoid this case.
900 */
901 if (fdp->fd_ofiles[old] != fp) {
902 /* we've allocated a descriptor which we won't use */
903 if (fdp->fd_ofiles[new] == NULL)
904 fdunused(fdp, new);
905 FILEDESC_XUNLOCK(fdp);
906 fdrop(fp, td);
907 return (EBADF);
908 }
909 KASSERT(old != new,
910 ("new fd is same as old"));
911
912 /*
913 * Save info on the descriptor being overwritten. We cannot close
914 * it without introducing an ownership race for the slot, since we
915 * need to drop the filedesc lock to call closef().
916 *
917 * XXX this duplicates parts of close().
918 */
919 delfp = fdp->fd_ofiles[new];
920 holdleaders = 0;
921 if (delfp != NULL) {
922 if (td->td_proc->p_fdtol != NULL) {
923 /*
924 * Ask fdfree() to sleep to ensure that all relevant
925 * process leaders can be traversed in closef().
926 */
927 fdp->fd_holdleaderscount++;
928 holdleaders = 1;
929 }
930 }
931
932 /*
933 * Duplicate the source descriptor
934 */
935 fdp->fd_ofiles[new] = fp;
936 if ((flags & DUP_CLOEXEC) != 0)
937 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] | UF_EXCLOSE;
938 else
939 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] & ~UF_EXCLOSE;
940 if (new > fdp->fd_lastfile)
941 fdp->fd_lastfile = new;
942 *retval = new;
943
944 /*
945 * If we dup'd over a valid file, we now own the reference to it
946 * and must dispose of it using closef() semantics (as if a
947 * close() were performed on it).
948 *
949 * XXX this duplicates parts of close().
950 */
951 if (delfp != NULL) {
952 knote_fdclose(td, new);
953 if (delfp->f_type == DTYPE_MQUEUE)
954 mq_fdclose(td, new, delfp);
955 FILEDESC_XUNLOCK(fdp);
956 (void) closef(delfp, td);
957 if (holdleaders) {
958 FILEDESC_XLOCK(fdp);
959 fdp->fd_holdleaderscount--;
960 if (fdp->fd_holdleaderscount == 0 &&
961 fdp->fd_holdleaderswakeup != 0) {
962 fdp->fd_holdleaderswakeup = 0;
963 wakeup(&fdp->fd_holdleaderscount);
964 }
965 FILEDESC_XUNLOCK(fdp);
966 }
967 } else {
968 FILEDESC_XUNLOCK(fdp);
969 }
970 return (0);
971 }
972
973 /*
974 * If sigio is on the list associated with a process or process group,
975 * disable signalling from the device, remove sigio from the list and
976 * free sigio.
977 */
978 void
979 funsetown(struct sigio **sigiop)
980 {
981 struct sigio *sigio;
982
983 SIGIO_LOCK();
984 sigio = *sigiop;
985 if (sigio == NULL) {
986 SIGIO_UNLOCK();
987 return;
988 }
989 *(sigio->sio_myref) = NULL;
990 if ((sigio)->sio_pgid < 0) {
991 struct pgrp *pg = (sigio)->sio_pgrp;
992 PGRP_LOCK(pg);
993 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
994 sigio, sio_pgsigio);
995 PGRP_UNLOCK(pg);
996 } else {
997 struct proc *p = (sigio)->sio_proc;
998 PROC_LOCK(p);
999 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
1000 sigio, sio_pgsigio);
1001 PROC_UNLOCK(p);
1002 }
1003 SIGIO_UNLOCK();
1004 crfree(sigio->sio_ucred);
1005 free(sigio, M_SIGIO);
1006 }
1007
1008 /*
1009 * Free a list of sigio structures.
1010 * We only need to lock the SIGIO_LOCK because we have made ourselves
1011 * inaccessible to callers of fsetown and therefore do not need to lock
1012 * the proc or pgrp struct for the list manipulation.
1013 */
1014 void
1015 funsetownlst(struct sigiolst *sigiolst)
1016 {
1017 struct proc *p;
1018 struct pgrp *pg;
1019 struct sigio *sigio;
1020
1021 sigio = SLIST_FIRST(sigiolst);
1022 if (sigio == NULL)
1023 return;
1024 p = NULL;
1025 pg = NULL;
1026
1027 /*
1028 * Every entry of the list should belong
1029 * to a single proc or pgrp.
1030 */
1031 if (sigio->sio_pgid < 0) {
1032 pg = sigio->sio_pgrp;
1033 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
1034 } else /* if (sigio->sio_pgid > 0) */ {
1035 p = sigio->sio_proc;
1036 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1037 }
1038
1039 SIGIO_LOCK();
1040 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
1041 *(sigio->sio_myref) = NULL;
1042 if (pg != NULL) {
1043 KASSERT(sigio->sio_pgid < 0,
1044 ("Proc sigio in pgrp sigio list"));
1045 KASSERT(sigio->sio_pgrp == pg,
1046 ("Bogus pgrp in sigio list"));
1047 PGRP_LOCK(pg);
1048 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
1049 sio_pgsigio);
1050 PGRP_UNLOCK(pg);
1051 } else /* if (p != NULL) */ {
1052 KASSERT(sigio->sio_pgid > 0,
1053 ("Pgrp sigio in proc sigio list"));
1054 KASSERT(sigio->sio_proc == p,
1055 ("Bogus proc in sigio list"));
1056 PROC_LOCK(p);
1057 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
1058 sio_pgsigio);
1059 PROC_UNLOCK(p);
1060 }
1061 SIGIO_UNLOCK();
1062 crfree(sigio->sio_ucred);
1063 free(sigio, M_SIGIO);
1064 SIGIO_LOCK();
1065 }
1066 SIGIO_UNLOCK();
1067 }
1068
1069 /*
1070 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
1071 *
1072 * After permission checking, add a sigio structure to the sigio list for
1073 * the process or process group.
1074 */
1075 int
1076 fsetown(pid_t pgid, struct sigio **sigiop)
1077 {
1078 struct proc *proc;
1079 struct pgrp *pgrp;
1080 struct sigio *sigio;
1081 int ret;
1082
1083 if (pgid == 0) {
1084 funsetown(sigiop);
1085 return (0);
1086 }
1087
1088 ret = 0;
1089
1090 /* Allocate and fill in the new sigio out of locks. */
1091 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
1092 sigio->sio_pgid = pgid;
1093 sigio->sio_ucred = crhold(curthread->td_ucred);
1094 sigio->sio_myref = sigiop;
1095
1096 sx_slock(&proctree_lock);
1097 if (pgid > 0) {
1098 proc = pfind(pgid);
1099 if (proc == NULL) {
1100 ret = ESRCH;
1101 goto fail;
1102 }
1103
1104 /*
1105 * Policy - Don't allow a process to FSETOWN a process
1106 * in another session.
1107 *
1108 * Remove this test to allow maximum flexibility or
1109 * restrict FSETOWN to the current process or process
1110 * group for maximum safety.
1111 */
1112 PROC_UNLOCK(proc);
1113 if (proc->p_session != curthread->td_proc->p_session) {
1114 ret = EPERM;
1115 goto fail;
1116 }
1117
1118 pgrp = NULL;
1119 } else /* if (pgid < 0) */ {
1120 pgrp = pgfind(-pgid);
1121 if (pgrp == NULL) {
1122 ret = ESRCH;
1123 goto fail;
1124 }
1125 PGRP_UNLOCK(pgrp);
1126
1127 /*
1128 * Policy - Don't allow a process to FSETOWN a process
1129 * in another session.
1130 *
1131 * Remove this test to allow maximum flexibility or
1132 * restrict FSETOWN to the current process or process
1133 * group for maximum safety.
1134 */
1135 if (pgrp->pg_session != curthread->td_proc->p_session) {
1136 ret = EPERM;
1137 goto fail;
1138 }
1139
1140 proc = NULL;
1141 }
1142 funsetown(sigiop);
1143 if (pgid > 0) {
1144 PROC_LOCK(proc);
1145 /*
1146 * Since funsetownlst() is called without the proctree
1147 * locked, we need to check for P_WEXIT.
1148 * XXX: is ESRCH correct?
1149 */
1150 if ((proc->p_flag & P_WEXIT) != 0) {
1151 PROC_UNLOCK(proc);
1152 ret = ESRCH;
1153 goto fail;
1154 }
1155 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1156 sigio->sio_proc = proc;
1157 PROC_UNLOCK(proc);
1158 } else {
1159 PGRP_LOCK(pgrp);
1160 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1161 sigio->sio_pgrp = pgrp;
1162 PGRP_UNLOCK(pgrp);
1163 }
1164 sx_sunlock(&proctree_lock);
1165 SIGIO_LOCK();
1166 *sigiop = sigio;
1167 SIGIO_UNLOCK();
1168 return (0);
1169
1170 fail:
1171 sx_sunlock(&proctree_lock);
1172 crfree(sigio->sio_ucred);
1173 free(sigio, M_SIGIO);
1174 return (ret);
1175 }
1176
1177 /*
1178 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1179 */
1180 pid_t
1181 fgetown(sigiop)
1182 struct sigio **sigiop;
1183 {
1184 pid_t pgid;
1185
1186 SIGIO_LOCK();
1187 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1188 SIGIO_UNLOCK();
1189 return (pgid);
1190 }
1191
1192 /*
1193 * Close a file descriptor.
1194 */
1195 #ifndef _SYS_SYSPROTO_H_
1196 struct close_args {
1197 int fd;
1198 };
1199 #endif
1200 /* ARGSUSED */
1201 int
1202 sys_close(td, uap)
1203 struct thread *td;
1204 struct close_args *uap;
1205 {
1206
1207 return (kern_close(td, uap->fd));
1208 }
1209
1210 int
1211 kern_close(td, fd)
1212 struct thread *td;
1213 int fd;
1214 {
1215 struct filedesc *fdp;
1216 struct file *fp, *fp_object;
1217 int error;
1218 int holdleaders;
1219
1220 error = 0;
1221 holdleaders = 0;
1222 fdp = td->td_proc->p_fd;
1223
1224 AUDIT_SYSCLOSE(td, fd);
1225
1226 FILEDESC_XLOCK(fdp);
1227 if ((unsigned)fd >= fdp->fd_nfiles ||
1228 (fp = fdp->fd_ofiles[fd]) == NULL) {
1229 FILEDESC_XUNLOCK(fdp);
1230 return (EBADF);
1231 }
1232 fdp->fd_ofiles[fd] = NULL;
1233 fdp->fd_ofileflags[fd] = 0;
1234 fdunused(fdp, fd);
1235 if (td->td_proc->p_fdtol != NULL) {
1236 /*
1237 * Ask fdfree() to sleep to ensure that all relevant
1238 * process leaders can be traversed in closef().
1239 */
1240 fdp->fd_holdleaderscount++;
1241 holdleaders = 1;
1242 }
1243
1244 /*
1245 * We now hold the fp reference that used to be owned by the
1246 * descriptor array. We have to unlock the FILEDESC *AFTER*
1247 * knote_fdclose to prevent a race of the fd getting opened, a knote
1248 * added, and deleteing a knote for the new fd.
1249 */
1250 knote_fdclose(td, fd);
1251
1252 /*
1253 * When we're closing an fd with a capability, we need to notify
1254 * mqueue if the underlying object is of type mqueue.
1255 */
1256 (void)cap_funwrap(fp, 0, &fp_object);
1257 if (fp_object->f_type == DTYPE_MQUEUE)
1258 mq_fdclose(td, fd, fp_object);
1259 FILEDESC_XUNLOCK(fdp);
1260
1261 error = closef(fp, td);
1262 if (holdleaders) {
1263 FILEDESC_XLOCK(fdp);
1264 fdp->fd_holdleaderscount--;
1265 if (fdp->fd_holdleaderscount == 0 &&
1266 fdp->fd_holdleaderswakeup != 0) {
1267 fdp->fd_holdleaderswakeup = 0;
1268 wakeup(&fdp->fd_holdleaderscount);
1269 }
1270 FILEDESC_XUNLOCK(fdp);
1271 }
1272 return (error);
1273 }
1274
1275 /*
1276 * Close open file descriptors.
1277 */
1278 #ifndef _SYS_SYSPROTO_H_
1279 struct closefrom_args {
1280 int lowfd;
1281 };
1282 #endif
1283 /* ARGSUSED */
1284 int
1285 sys_closefrom(struct thread *td, struct closefrom_args *uap)
1286 {
1287 struct filedesc *fdp;
1288 int fd;
1289
1290 fdp = td->td_proc->p_fd;
1291 AUDIT_ARG_FD(uap->lowfd);
1292
1293 /*
1294 * Treat negative starting file descriptor values identical to
1295 * closefrom(0) which closes all files.
1296 */
1297 if (uap->lowfd < 0)
1298 uap->lowfd = 0;
1299 FILEDESC_SLOCK(fdp);
1300 for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1301 if (fdp->fd_ofiles[fd] != NULL) {
1302 FILEDESC_SUNLOCK(fdp);
1303 (void)kern_close(td, fd);
1304 FILEDESC_SLOCK(fdp);
1305 }
1306 }
1307 FILEDESC_SUNLOCK(fdp);
1308 return (0);
1309 }
1310
1311 #if defined(COMPAT_43)
1312 /*
1313 * Return status information about a file descriptor.
1314 */
1315 #ifndef _SYS_SYSPROTO_H_
1316 struct ofstat_args {
1317 int fd;
1318 struct ostat *sb;
1319 };
1320 #endif
1321 /* ARGSUSED */
1322 int
1323 ofstat(struct thread *td, struct ofstat_args *uap)
1324 {
1325 struct ostat oub;
1326 struct stat ub;
1327 int error;
1328
1329 error = kern_fstat(td, uap->fd, &ub);
1330 if (error == 0) {
1331 cvtstat(&ub, &oub);
1332 error = copyout(&oub, uap->sb, sizeof(oub));
1333 }
1334 return (error);
1335 }
1336 #endif /* COMPAT_43 */
1337
1338 /*
1339 * Return status information about a file descriptor.
1340 */
1341 #ifndef _SYS_SYSPROTO_H_
1342 struct fstat_args {
1343 int fd;
1344 struct stat *sb;
1345 };
1346 #endif
1347 /* ARGSUSED */
1348 int
1349 sys_fstat(struct thread *td, struct fstat_args *uap)
1350 {
1351 struct stat ub;
1352 int error;
1353
1354 error = kern_fstat(td, uap->fd, &ub);
1355 if (error == 0)
1356 error = copyout(&ub, uap->sb, sizeof(ub));
1357 return (error);
1358 }
1359
1360 int
1361 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1362 {
1363 struct file *fp;
1364 int error;
1365
1366 AUDIT_ARG_FD(fd);
1367
1368 if ((error = fget(td, fd, CAP_FSTAT, &fp)) != 0)
1369 return (error);
1370
1371 AUDIT_ARG_FILE(td->td_proc, fp);
1372
1373 error = fo_stat(fp, sbp, td->td_ucred, td);
1374 fdrop(fp, td);
1375 #ifdef KTRACE
1376 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1377 ktrstat(sbp);
1378 #endif
1379 return (error);
1380 }
1381
1382 /*
1383 * Return status information about a file descriptor.
1384 */
1385 #ifndef _SYS_SYSPROTO_H_
1386 struct nfstat_args {
1387 int fd;
1388 struct nstat *sb;
1389 };
1390 #endif
1391 /* ARGSUSED */
1392 int
1393 sys_nfstat(struct thread *td, struct nfstat_args *uap)
1394 {
1395 struct nstat nub;
1396 struct stat ub;
1397 int error;
1398
1399 error = kern_fstat(td, uap->fd, &ub);
1400 if (error == 0) {
1401 cvtnstat(&ub, &nub);
1402 error = copyout(&nub, uap->sb, sizeof(nub));
1403 }
1404 return (error);
1405 }
1406
1407 /*
1408 * Return pathconf information about a file descriptor.
1409 */
1410 #ifndef _SYS_SYSPROTO_H_
1411 struct fpathconf_args {
1412 int fd;
1413 int name;
1414 };
1415 #endif
1416 /* ARGSUSED */
1417 int
1418 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
1419 {
1420 struct file *fp;
1421 struct vnode *vp;
1422 int error;
1423
1424 if ((error = fget(td, uap->fd, CAP_FPATHCONF, &fp)) != 0)
1425 return (error);
1426
1427 /* If asynchronous I/O is available, it works for all descriptors. */
1428 if (uap->name == _PC_ASYNC_IO) {
1429 td->td_retval[0] = async_io_version;
1430 goto out;
1431 }
1432 vp = fp->f_vnode;
1433 if (vp != NULL) {
1434 int vfslocked;
1435 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1436 vn_lock(vp, LK_SHARED | LK_RETRY);
1437 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1438 VOP_UNLOCK(vp, 0);
1439 VFS_UNLOCK_GIANT(vfslocked);
1440 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1441 if (uap->name != _PC_PIPE_BUF) {
1442 error = EINVAL;
1443 } else {
1444 td->td_retval[0] = PIPE_BUF;
1445 error = 0;
1446 }
1447 } else {
1448 error = EOPNOTSUPP;
1449 }
1450 out:
1451 fdrop(fp, td);
1452 return (error);
1453 }
1454
1455 /*
1456 * Grow the file table to accomodate (at least) nfd descriptors. This may
1457 * block and drop the filedesc lock, but it will reacquire it before
1458 * returning.
1459 */
1460 static void
1461 fdgrowtable(struct filedesc *fdp, int nfd)
1462 {
1463 struct filedesc0 *fdp0;
1464 struct freetable *fo;
1465 struct file **ntable;
1466 struct file **otable;
1467 char *nfileflags;
1468 int nnfiles, onfiles;
1469 NDSLOTTYPE *nmap;
1470
1471 FILEDESC_XLOCK_ASSERT(fdp);
1472
1473 KASSERT(fdp->fd_nfiles > 0,
1474 ("zero-length file table"));
1475
1476 /* compute the size of the new table */
1477 onfiles = fdp->fd_nfiles;
1478 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1479 if (nnfiles <= onfiles)
1480 /* the table is already large enough */
1481 return;
1482
1483 /* allocate a new table and (if required) new bitmaps */
1484 FILEDESC_XUNLOCK(fdp);
1485 ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable),
1486 M_FILEDESC, M_ZERO | M_WAITOK);
1487 nfileflags = (char *)&ntable[nnfiles];
1488 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1489 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE,
1490 M_FILEDESC, M_ZERO | M_WAITOK);
1491 else
1492 nmap = NULL;
1493 FILEDESC_XLOCK(fdp);
1494
1495 /*
1496 * We now have new tables ready to go. Since we dropped the
1497 * filedesc lock to call malloc(), watch out for a race.
1498 */
1499 onfiles = fdp->fd_nfiles;
1500 if (onfiles >= nnfiles) {
1501 /* we lost the race, but that's OK */
1502 free(ntable, M_FILEDESC);
1503 if (nmap != NULL)
1504 free(nmap, M_FILEDESC);
1505 return;
1506 }
1507 bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1508 bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1509 otable = fdp->fd_ofiles;
1510 fdp->fd_ofileflags = nfileflags;
1511 fdp->fd_ofiles = ntable;
1512 /*
1513 * We must preserve ofiles until the process exits because we can't
1514 * be certain that no threads have references to the old table via
1515 * _fget().
1516 */
1517 if (onfiles > NDFILE) {
1518 fo = (struct freetable *)&otable[onfiles];
1519 fdp0 = (struct filedesc0 *)fdp;
1520 fo->ft_table = otable;
1521 SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next);
1522 }
1523 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1524 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1525 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1526 free(fdp->fd_map, M_FILEDESC);
1527 fdp->fd_map = nmap;
1528 }
1529 fdp->fd_nfiles = nnfiles;
1530 }
1531
1532 /*
1533 * Allocate a file descriptor for the process.
1534 */
1535 int
1536 fdalloc(struct thread *td, int minfd, int *result)
1537 {
1538 struct proc *p = td->td_proc;
1539 struct filedesc *fdp = p->p_fd;
1540 int fd = -1, maxfd;
1541 #ifdef RACCT
1542 int error;
1543 #endif
1544
1545 FILEDESC_XLOCK_ASSERT(fdp);
1546
1547 if (fdp->fd_freefile > minfd)
1548 minfd = fdp->fd_freefile;
1549
1550 PROC_LOCK(p);
1551 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1552 PROC_UNLOCK(p);
1553
1554 /*
1555 * Search the bitmap for a free descriptor. If none is found, try
1556 * to grow the file table. Keep at it until we either get a file
1557 * descriptor or run into process or system limits; fdgrowtable()
1558 * may drop the filedesc lock, so we're in a race.
1559 */
1560 for (;;) {
1561 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1562 if (fd >= maxfd)
1563 return (EMFILE);
1564 if (fd < fdp->fd_nfiles)
1565 break;
1566 #ifdef RACCT
1567 PROC_LOCK(p);
1568 error = racct_set(p, RACCT_NOFILE, min(fdp->fd_nfiles * 2, maxfd));
1569 PROC_UNLOCK(p);
1570 if (error != 0)
1571 return (EMFILE);
1572 #endif
1573 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
1574 }
1575
1576 /*
1577 * Perform some sanity checks, then mark the file descriptor as
1578 * used and return it to the caller.
1579 */
1580 KASSERT(!fdisused(fdp, fd),
1581 ("fd_first_free() returned non-free descriptor"));
1582 KASSERT(fdp->fd_ofiles[fd] == NULL,
1583 ("free descriptor isn't"));
1584 fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
1585 fdused(fdp, fd);
1586 *result = fd;
1587 return (0);
1588 }
1589
1590 /*
1591 * Allocate n file descriptors for the process.
1592 */
1593 int
1594 fdallocn(struct thread *td, int minfd, int *fds, int n)
1595 {
1596 struct proc *p = td->td_proc;
1597 struct filedesc *fdp = p->p_fd;
1598 int i;
1599
1600 FILEDESC_XLOCK_ASSERT(fdp);
1601
1602 if (!fdavail(td, n))
1603 return (EMFILE);
1604
1605 for (i = 0; i < n; i++)
1606 if (fdalloc(td, 0, &fds[i]) != 0)
1607 break;
1608
1609 if (i < n) {
1610 for (i--; i >= 0; i--)
1611 fdunused(fdp, fds[i]);
1612 return (EMFILE);
1613 }
1614
1615 return (0);
1616 }
1617
1618 /*
1619 * Check to see whether n user file descriptors are available to the process
1620 * p.
1621 */
1622 int
1623 fdavail(struct thread *td, int n)
1624 {
1625 struct proc *p = td->td_proc;
1626 struct filedesc *fdp = td->td_proc->p_fd;
1627 struct file **fpp;
1628 int i, lim, last;
1629
1630 FILEDESC_LOCK_ASSERT(fdp);
1631
1632 /*
1633 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1634 * call racct_add() from there instead of dealing with containers
1635 * here.
1636 */
1637 PROC_LOCK(p);
1638 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1639 PROC_UNLOCK(p);
1640 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1641 return (1);
1642 last = min(fdp->fd_nfiles, lim);
1643 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1644 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1645 if (*fpp == NULL && --n <= 0)
1646 return (1);
1647 }
1648 return (0);
1649 }
1650
1651 /*
1652 * Create a new open file structure and allocate a file decriptor for the
1653 * process that refers to it. We add one reference to the file for the
1654 * descriptor table and one reference for resultfp. This is to prevent us
1655 * being preempted and the entry in the descriptor table closed after we
1656 * release the FILEDESC lock.
1657 */
1658 int
1659 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1660 {
1661 struct file *fp;
1662 int error, fd;
1663
1664 error = falloc_noinstall(td, &fp);
1665 if (error)
1666 return (error); /* no reference held on error */
1667
1668 error = finstall(td, fp, &fd, flags);
1669 if (error) {
1670 fdrop(fp, td); /* one reference (fp only) */
1671 return (error);
1672 }
1673
1674 if (resultfp != NULL)
1675 *resultfp = fp; /* copy out result */
1676 else
1677 fdrop(fp, td); /* release local reference */
1678
1679 if (resultfd != NULL)
1680 *resultfd = fd;
1681
1682 return (0);
1683 }
1684
1685 /*
1686 * Create a new open file structure without allocating a file descriptor.
1687 */
1688 int
1689 falloc_noinstall(struct thread *td, struct file **resultfp)
1690 {
1691 struct file *fp;
1692 int maxuserfiles = maxfiles - (maxfiles / 20);
1693 static struct timeval lastfail;
1694 static int curfail;
1695
1696 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1697
1698 if ((openfiles >= maxuserfiles &&
1699 priv_check(td, PRIV_MAXFILES) != 0) ||
1700 openfiles >= maxfiles) {
1701 if (ppsratecheck(&lastfail, &curfail, 1)) {
1702 printf("kern.maxfiles limit exceeded by uid %i, "
1703 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1704 }
1705 return (ENFILE);
1706 }
1707 atomic_add_int(&openfiles, 1);
1708 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1709 refcount_init(&fp->f_count, 1);
1710 fp->f_cred = crhold(td->td_ucred);
1711 fp->f_ops = &badfileops;
1712 fp->f_data = NULL;
1713 fp->f_vnode = NULL;
1714 *resultfp = fp;
1715 return (0);
1716 }
1717
1718 /*
1719 * Install a file in a file descriptor table.
1720 */
1721 int
1722 finstall(struct thread *td, struct file *fp, int *fd, int flags)
1723 {
1724 struct filedesc *fdp = td->td_proc->p_fd;
1725 int error;
1726
1727 KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1728 KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1729
1730 FILEDESC_XLOCK(fdp);
1731 if ((error = fdalloc(td, 0, fd))) {
1732 FILEDESC_XUNLOCK(fdp);
1733 return (error);
1734 }
1735 fhold(fp);
1736 fdp->fd_ofiles[*fd] = fp;
1737 if ((flags & O_CLOEXEC) != 0)
1738 fdp->fd_ofileflags[*fd] |= UF_EXCLOSE;
1739 FILEDESC_XUNLOCK(fdp);
1740 return (0);
1741 }
1742
1743 /*
1744 * Build a new filedesc structure from another.
1745 * Copy the current, root, and jail root vnode references.
1746 */
1747 struct filedesc *
1748 fdinit(struct filedesc *fdp)
1749 {
1750 struct filedesc0 *newfdp;
1751
1752 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1753 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1754 if (fdp != NULL) {
1755 FILEDESC_XLOCK(fdp);
1756 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1757 if (newfdp->fd_fd.fd_cdir)
1758 VREF(newfdp->fd_fd.fd_cdir);
1759 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1760 if (newfdp->fd_fd.fd_rdir)
1761 VREF(newfdp->fd_fd.fd_rdir);
1762 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1763 if (newfdp->fd_fd.fd_jdir)
1764 VREF(newfdp->fd_fd.fd_jdir);
1765 FILEDESC_XUNLOCK(fdp);
1766 }
1767
1768 /* Create the file descriptor table. */
1769 newfdp->fd_fd.fd_refcnt = 1;
1770 newfdp->fd_fd.fd_holdcnt = 1;
1771 newfdp->fd_fd.fd_cmask = CMASK;
1772 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1773 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1774 newfdp->fd_fd.fd_nfiles = NDFILE;
1775 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1776 newfdp->fd_fd.fd_lastfile = -1;
1777 return (&newfdp->fd_fd);
1778 }
1779
1780 static struct filedesc *
1781 fdhold(struct proc *p)
1782 {
1783 struct filedesc *fdp;
1784
1785 mtx_lock(&fdesc_mtx);
1786 fdp = p->p_fd;
1787 if (fdp != NULL)
1788 fdp->fd_holdcnt++;
1789 mtx_unlock(&fdesc_mtx);
1790 return (fdp);
1791 }
1792
1793 static void
1794 fddrop(struct filedesc *fdp)
1795 {
1796 struct filedesc0 *fdp0;
1797 struct freetable *ft;
1798 int i;
1799
1800 mtx_lock(&fdesc_mtx);
1801 i = --fdp->fd_holdcnt;
1802 mtx_unlock(&fdesc_mtx);
1803 if (i > 0)
1804 return;
1805
1806 FILEDESC_LOCK_DESTROY(fdp);
1807 fdp0 = (struct filedesc0 *)fdp;
1808 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1809 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1810 free(ft->ft_table, M_FILEDESC);
1811 }
1812 free(fdp, M_FILEDESC);
1813 }
1814
1815 /*
1816 * Share a filedesc structure.
1817 */
1818 struct filedesc *
1819 fdshare(struct filedesc *fdp)
1820 {
1821
1822 FILEDESC_XLOCK(fdp);
1823 fdp->fd_refcnt++;
1824 FILEDESC_XUNLOCK(fdp);
1825 return (fdp);
1826 }
1827
1828 /*
1829 * Unshare a filedesc structure, if necessary by making a copy
1830 */
1831 void
1832 fdunshare(struct proc *p, struct thread *td)
1833 {
1834
1835 FILEDESC_XLOCK(p->p_fd);
1836 if (p->p_fd->fd_refcnt > 1) {
1837 struct filedesc *tmp;
1838
1839 FILEDESC_XUNLOCK(p->p_fd);
1840 tmp = fdcopy(p->p_fd);
1841 fdfree(td);
1842 p->p_fd = tmp;
1843 } else
1844 FILEDESC_XUNLOCK(p->p_fd);
1845 }
1846
1847 /*
1848 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1849 * this is to ease callers, not catch errors.
1850 */
1851 struct filedesc *
1852 fdcopy(struct filedesc *fdp)
1853 {
1854 struct filedesc *newfdp;
1855 int i;
1856
1857 /* Certain daemons might not have file descriptors. */
1858 if (fdp == NULL)
1859 return (NULL);
1860
1861 newfdp = fdinit(fdp);
1862 FILEDESC_SLOCK(fdp);
1863 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1864 FILEDESC_SUNLOCK(fdp);
1865 FILEDESC_XLOCK(newfdp);
1866 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1867 FILEDESC_XUNLOCK(newfdp);
1868 FILEDESC_SLOCK(fdp);
1869 }
1870 /* copy all passable descriptors (i.e. not kqueue) */
1871 newfdp->fd_freefile = -1;
1872 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1873 if (fdisused(fdp, i) &&
1874 (fdp->fd_ofiles[i]->f_ops->fo_flags & DFLAG_PASSABLE) &&
1875 fdp->fd_ofiles[i]->f_ops != &badfileops) {
1876 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1877 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1878 fhold(newfdp->fd_ofiles[i]);
1879 newfdp->fd_lastfile = i;
1880 } else {
1881 if (newfdp->fd_freefile == -1)
1882 newfdp->fd_freefile = i;
1883 }
1884 }
1885 newfdp->fd_cmask = fdp->fd_cmask;
1886 FILEDESC_SUNLOCK(fdp);
1887 FILEDESC_XLOCK(newfdp);
1888 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1889 if (newfdp->fd_ofiles[i] != NULL)
1890 fdused(newfdp, i);
1891 if (newfdp->fd_freefile == -1)
1892 newfdp->fd_freefile = i;
1893 FILEDESC_XUNLOCK(newfdp);
1894 return (newfdp);
1895 }
1896
1897 /*
1898 * Release a filedesc structure.
1899 */
1900 void
1901 fdfree(struct thread *td)
1902 {
1903 struct filedesc *fdp;
1904 struct file **fpp;
1905 int i, locked;
1906 struct filedesc_to_leader *fdtol;
1907 struct file *fp;
1908 struct vnode *cdir, *jdir, *rdir, *vp;
1909 struct flock lf;
1910
1911 /* Certain daemons might not have file descriptors. */
1912 fdp = td->td_proc->p_fd;
1913 if (fdp == NULL)
1914 return;
1915
1916 #ifdef RACCT
1917 PROC_LOCK(td->td_proc);
1918 racct_set(td->td_proc, RACCT_NOFILE, 0);
1919 PROC_UNLOCK(td->td_proc);
1920 #endif
1921
1922 /* Check for special need to clear POSIX style locks */
1923 fdtol = td->td_proc->p_fdtol;
1924 if (fdtol != NULL) {
1925 FILEDESC_XLOCK(fdp);
1926 KASSERT(fdtol->fdl_refcount > 0,
1927 ("filedesc_to_refcount botch: fdl_refcount=%d",
1928 fdtol->fdl_refcount));
1929 if (fdtol->fdl_refcount == 1 &&
1930 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1931 for (i = 0, fpp = fdp->fd_ofiles;
1932 i <= fdp->fd_lastfile;
1933 i++, fpp++) {
1934 if (*fpp == NULL ||
1935 (*fpp)->f_type != DTYPE_VNODE)
1936 continue;
1937 fp = *fpp;
1938 fhold(fp);
1939 FILEDESC_XUNLOCK(fdp);
1940 lf.l_whence = SEEK_SET;
1941 lf.l_start = 0;
1942 lf.l_len = 0;
1943 lf.l_type = F_UNLCK;
1944 vp = fp->f_vnode;
1945 locked = VFS_LOCK_GIANT(vp->v_mount);
1946 (void) VOP_ADVLOCK(vp,
1947 (caddr_t)td->td_proc->
1948 p_leader,
1949 F_UNLCK,
1950 &lf,
1951 F_POSIX);
1952 VFS_UNLOCK_GIANT(locked);
1953 FILEDESC_XLOCK(fdp);
1954 fdrop(fp, td);
1955 fpp = fdp->fd_ofiles + i;
1956 }
1957 }
1958 retry:
1959 if (fdtol->fdl_refcount == 1) {
1960 if (fdp->fd_holdleaderscount > 0 &&
1961 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1962 /*
1963 * close() or do_dup() has cleared a reference
1964 * in a shared file descriptor table.
1965 */
1966 fdp->fd_holdleaderswakeup = 1;
1967 sx_sleep(&fdp->fd_holdleaderscount,
1968 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1969 goto retry;
1970 }
1971 if (fdtol->fdl_holdcount > 0) {
1972 /*
1973 * Ensure that fdtol->fdl_leader remains
1974 * valid in closef().
1975 */
1976 fdtol->fdl_wakeup = 1;
1977 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1978 "fdlhold", 0);
1979 goto retry;
1980 }
1981 }
1982 fdtol->fdl_refcount--;
1983 if (fdtol->fdl_refcount == 0 &&
1984 fdtol->fdl_holdcount == 0) {
1985 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1986 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1987 } else
1988 fdtol = NULL;
1989 td->td_proc->p_fdtol = NULL;
1990 FILEDESC_XUNLOCK(fdp);
1991 if (fdtol != NULL)
1992 free(fdtol, M_FILEDESC_TO_LEADER);
1993 }
1994 FILEDESC_XLOCK(fdp);
1995 i = --fdp->fd_refcnt;
1996 FILEDESC_XUNLOCK(fdp);
1997 if (i > 0)
1998 return;
1999
2000 fpp = fdp->fd_ofiles;
2001 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
2002 if (*fpp) {
2003 FILEDESC_XLOCK(fdp);
2004 fp = *fpp;
2005 *fpp = NULL;
2006 FILEDESC_XUNLOCK(fdp);
2007 (void) closef(fp, td);
2008 }
2009 }
2010 FILEDESC_XLOCK(fdp);
2011
2012 /* XXX This should happen earlier. */
2013 mtx_lock(&fdesc_mtx);
2014 td->td_proc->p_fd = NULL;
2015 mtx_unlock(&fdesc_mtx);
2016
2017 if (fdp->fd_nfiles > NDFILE)
2018 free(fdp->fd_ofiles, M_FILEDESC);
2019 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
2020 free(fdp->fd_map, M_FILEDESC);
2021
2022 fdp->fd_nfiles = 0;
2023
2024 cdir = fdp->fd_cdir;
2025 fdp->fd_cdir = NULL;
2026 rdir = fdp->fd_rdir;
2027 fdp->fd_rdir = NULL;
2028 jdir = fdp->fd_jdir;
2029 fdp->fd_jdir = NULL;
2030 FILEDESC_XUNLOCK(fdp);
2031
2032 if (cdir) {
2033 locked = VFS_LOCK_GIANT(cdir->v_mount);
2034 vrele(cdir);
2035 VFS_UNLOCK_GIANT(locked);
2036 }
2037 if (rdir) {
2038 locked = VFS_LOCK_GIANT(rdir->v_mount);
2039 vrele(rdir);
2040 VFS_UNLOCK_GIANT(locked);
2041 }
2042 if (jdir) {
2043 locked = VFS_LOCK_GIANT(jdir->v_mount);
2044 vrele(jdir);
2045 VFS_UNLOCK_GIANT(locked);
2046 }
2047
2048 fddrop(fdp);
2049 }
2050
2051 /*
2052 * For setugid programs, we don't want to people to use that setugidness
2053 * to generate error messages which write to a file which otherwise would
2054 * otherwise be off-limits to the process. We check for filesystems where
2055 * the vnode can change out from under us after execve (like [lin]procfs).
2056 *
2057 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
2058 * sufficient. We also don't check for setugidness since we know we are.
2059 */
2060 static int
2061 is_unsafe(struct file *fp)
2062 {
2063 if (fp->f_type == DTYPE_VNODE) {
2064 struct vnode *vp = fp->f_vnode;
2065
2066 if ((vp->v_vflag & VV_PROCDEP) != 0)
2067 return (1);
2068 }
2069 return (0);
2070 }
2071
2072 /*
2073 * Make this setguid thing safe, if at all possible.
2074 */
2075 void
2076 setugidsafety(struct thread *td)
2077 {
2078 struct filedesc *fdp;
2079 int i;
2080
2081 /* Certain daemons might not have file descriptors. */
2082 fdp = td->td_proc->p_fd;
2083 if (fdp == NULL)
2084 return;
2085
2086 /*
2087 * Note: fdp->fd_ofiles may be reallocated out from under us while
2088 * we are blocked in a close. Be careful!
2089 */
2090 FILEDESC_XLOCK(fdp);
2091 for (i = 0; i <= fdp->fd_lastfile; i++) {
2092 if (i > 2)
2093 break;
2094 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
2095 struct file *fp;
2096
2097 knote_fdclose(td, i);
2098 /*
2099 * NULL-out descriptor prior to close to avoid
2100 * a race while close blocks.
2101 */
2102 fp = fdp->fd_ofiles[i];
2103 fdp->fd_ofiles[i] = NULL;
2104 fdp->fd_ofileflags[i] = 0;
2105 fdunused(fdp, i);
2106 FILEDESC_XUNLOCK(fdp);
2107 (void) closef(fp, td);
2108 FILEDESC_XLOCK(fdp);
2109 }
2110 }
2111 FILEDESC_XUNLOCK(fdp);
2112 }
2113
2114 /*
2115 * If a specific file object occupies a specific file descriptor, close the
2116 * file descriptor entry and drop a reference on the file object. This is a
2117 * convenience function to handle a subsequent error in a function that calls
2118 * falloc() that handles the race that another thread might have closed the
2119 * file descriptor out from under the thread creating the file object.
2120 */
2121 void
2122 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2123 {
2124
2125 FILEDESC_XLOCK(fdp);
2126 if (fdp->fd_ofiles[idx] == fp) {
2127 fdp->fd_ofiles[idx] = NULL;
2128 fdunused(fdp, idx);
2129 FILEDESC_XUNLOCK(fdp);
2130 fdrop(fp, td);
2131 } else
2132 FILEDESC_XUNLOCK(fdp);
2133 }
2134
2135 /*
2136 * Close any files on exec?
2137 */
2138 void
2139 fdcloseexec(struct thread *td)
2140 {
2141 struct filedesc *fdp;
2142 int i;
2143
2144 /* Certain daemons might not have file descriptors. */
2145 fdp = td->td_proc->p_fd;
2146 if (fdp == NULL)
2147 return;
2148
2149 FILEDESC_XLOCK(fdp);
2150
2151 /*
2152 * We cannot cache fd_ofiles or fd_ofileflags since operations
2153 * may block and rip them out from under us.
2154 */
2155 for (i = 0; i <= fdp->fd_lastfile; i++) {
2156 if (fdp->fd_ofiles[i] != NULL &&
2157 (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
2158 (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
2159 struct file *fp;
2160
2161 knote_fdclose(td, i);
2162 /*
2163 * NULL-out descriptor prior to close to avoid
2164 * a race while close blocks.
2165 */
2166 fp = fdp->fd_ofiles[i];
2167 fdp->fd_ofiles[i] = NULL;
2168 fdp->fd_ofileflags[i] = 0;
2169 fdunused(fdp, i);
2170 if (fp->f_type == DTYPE_MQUEUE)
2171 mq_fdclose(td, i, fp);
2172 FILEDESC_XUNLOCK(fdp);
2173 (void) closef(fp, td);
2174 FILEDESC_XLOCK(fdp);
2175 }
2176 }
2177 FILEDESC_XUNLOCK(fdp);
2178 }
2179
2180 /*
2181 * It is unsafe for set[ug]id processes to be started with file
2182 * descriptors 0..2 closed, as these descriptors are given implicit
2183 * significance in the Standard C library. fdcheckstd() will create a
2184 * descriptor referencing /dev/null for each of stdin, stdout, and
2185 * stderr that is not already open.
2186 */
2187 int
2188 fdcheckstd(struct thread *td)
2189 {
2190 struct filedesc *fdp;
2191 register_t retval, save;
2192 int i, error, devnull;
2193
2194 fdp = td->td_proc->p_fd;
2195 if (fdp == NULL)
2196 return (0);
2197 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2198 devnull = -1;
2199 error = 0;
2200 for (i = 0; i < 3; i++) {
2201 if (fdp->fd_ofiles[i] != NULL)
2202 continue;
2203 if (devnull < 0) {
2204 save = td->td_retval[0];
2205 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2206 O_RDWR, 0);
2207 devnull = td->td_retval[0];
2208 td->td_retval[0] = save;
2209 if (error)
2210 break;
2211 KASSERT(devnull == i, ("oof, we didn't get our fd"));
2212 } else {
2213 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2214 if (error != 0)
2215 break;
2216 }
2217 }
2218 return (error);
2219 }
2220
2221 /*
2222 * Internal form of close. Decrement reference count on file structure.
2223 * Note: td may be NULL when closing a file that was being passed in a
2224 * message.
2225 *
2226 * XXXRW: Giant is not required for the caller, but often will be held; this
2227 * makes it moderately likely the Giant will be recursed in the VFS case.
2228 */
2229 int
2230 closef(struct file *fp, struct thread *td)
2231 {
2232 struct vnode *vp;
2233 struct flock lf;
2234 struct filedesc_to_leader *fdtol;
2235 struct filedesc *fdp;
2236 struct file *fp_object;
2237
2238 /*
2239 * POSIX record locking dictates that any close releases ALL
2240 * locks owned by this process. This is handled by setting
2241 * a flag in the unlock to free ONLY locks obeying POSIX
2242 * semantics, and not to free BSD-style file locks.
2243 * If the descriptor was in a message, POSIX-style locks
2244 * aren't passed with the descriptor, and the thread pointer
2245 * will be NULL. Callers should be careful only to pass a
2246 * NULL thread pointer when there really is no owning
2247 * context that might have locks, or the locks will be
2248 * leaked.
2249 *
2250 * If this is a capability, we do lock processing under the underlying
2251 * node, not the capability itself.
2252 */
2253 (void)cap_funwrap(fp, 0, &fp_object);
2254 if ((fp_object->f_type == DTYPE_VNODE) && (td != NULL)) {
2255 int vfslocked;
2256
2257 vp = fp_object->f_vnode;
2258 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2259 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2260 lf.l_whence = SEEK_SET;
2261 lf.l_start = 0;
2262 lf.l_len = 0;
2263 lf.l_type = F_UNLCK;
2264 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2265 F_UNLCK, &lf, F_POSIX);
2266 }
2267 fdtol = td->td_proc->p_fdtol;
2268 if (fdtol != NULL) {
2269 /*
2270 * Handle special case where file descriptor table is
2271 * shared between multiple process leaders.
2272 */
2273 fdp = td->td_proc->p_fd;
2274 FILEDESC_XLOCK(fdp);
2275 for (fdtol = fdtol->fdl_next;
2276 fdtol != td->td_proc->p_fdtol;
2277 fdtol = fdtol->fdl_next) {
2278 if ((fdtol->fdl_leader->p_flag &
2279 P_ADVLOCK) == 0)
2280 continue;
2281 fdtol->fdl_holdcount++;
2282 FILEDESC_XUNLOCK(fdp);
2283 lf.l_whence = SEEK_SET;
2284 lf.l_start = 0;
2285 lf.l_len = 0;
2286 lf.l_type = F_UNLCK;
2287 vp = fp_object->f_vnode;
2288 (void) VOP_ADVLOCK(vp,
2289 (caddr_t)fdtol->fdl_leader,
2290 F_UNLCK, &lf, F_POSIX);
2291 FILEDESC_XLOCK(fdp);
2292 fdtol->fdl_holdcount--;
2293 if (fdtol->fdl_holdcount == 0 &&
2294 fdtol->fdl_wakeup != 0) {
2295 fdtol->fdl_wakeup = 0;
2296 wakeup(fdtol);
2297 }
2298 }
2299 FILEDESC_XUNLOCK(fdp);
2300 }
2301 VFS_UNLOCK_GIANT(vfslocked);
2302 }
2303 return (fdrop(fp, td));
2304 }
2305
2306 /*
2307 * Initialize the file pointer with the specified properties.
2308 *
2309 * The ops are set with release semantics to be certain that the flags, type,
2310 * and data are visible when ops is. This is to prevent ops methods from being
2311 * called with bad data.
2312 */
2313 void
2314 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2315 {
2316 fp->f_data = data;
2317 fp->f_flag = flag;
2318 fp->f_type = type;
2319 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2320 }
2321
2322 struct file *
2323 fget_unlocked(struct filedesc *fdp, int fd)
2324 {
2325 struct file *fp;
2326 u_int count;
2327
2328 if (fd < 0 || fd >= fdp->fd_nfiles)
2329 return (NULL);
2330 /*
2331 * Fetch the descriptor locklessly. We avoid fdrop() races by
2332 * never raising a refcount above 0. To accomplish this we have
2333 * to use a cmpset loop rather than an atomic_add. The descriptor
2334 * must be re-verified once we acquire a reference to be certain
2335 * that the identity is still correct and we did not lose a race
2336 * due to preemption.
2337 */
2338 for (;;) {
2339 fp = fdp->fd_ofiles[fd];
2340 if (fp == NULL)
2341 break;
2342 count = fp->f_count;
2343 if (count == 0)
2344 continue;
2345 /*
2346 * Use an acquire barrier to prevent caching of fd_ofiles
2347 * so it is refreshed for verification.
2348 */
2349 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2350 continue;
2351 if (fp == fdp->fd_ofiles[fd])
2352 break;
2353 fdrop(fp, curthread);
2354 }
2355
2356 return (fp);
2357 }
2358
2359 /*
2360 * Extract the file pointer associated with the specified descriptor for the
2361 * current user process.
2362 *
2363 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2364 * returned.
2365 *
2366 * If the FGET_GETCAP flag is set, the capability itself will be returned.
2367 * Calling _fget() with FGET_GETCAP on a non-capability will return EINVAL.
2368 * Otherwise, if the file is a capability, its rights will be checked against
2369 * the capability rights mask, and if successful, the object will be unwrapped.
2370 *
2371 * If an error occured the non-zero error is returned and *fpp is set to
2372 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is
2373 * responsible for fdrop().
2374 */
2375 #define FGET_GETCAP 0x00000001
2376 static __inline int
2377 _fget(struct thread *td, int fd, struct file **fpp, int flags,
2378 cap_rights_t needrights, cap_rights_t *haverightsp, u_char *maxprotp,
2379 int fget_flags)
2380 {
2381 struct filedesc *fdp;
2382 struct file *fp;
2383 #ifdef CAPABILITIES
2384 struct file *fp_fromcap;
2385 #endif
2386 int error;
2387
2388 *fpp = NULL;
2389 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2390 return (EBADF);
2391 if ((fp = fget_unlocked(fdp, fd)) == NULL)
2392 return (EBADF);
2393 if (fp->f_ops == &badfileops) {
2394 fdrop(fp, td);
2395 return (EBADF);
2396 }
2397
2398 #ifdef CAPABILITIES
2399 /*
2400 * If this is a capability, what rights does it have?
2401 */
2402 if (haverightsp != NULL) {
2403 if (fp->f_type == DTYPE_CAPABILITY)
2404 *haverightsp = cap_rights(fp);
2405 else
2406 *haverightsp = CAP_MASK_VALID;
2407 }
2408
2409 /*
2410 * If a capability has been requested, return the capability directly.
2411 * Otherwise, check capability rights, extract the underlying object,
2412 * and check its access flags.
2413 */
2414 if (fget_flags & FGET_GETCAP) {
2415 if (fp->f_type != DTYPE_CAPABILITY) {
2416 fdrop(fp, td);
2417 return (EINVAL);
2418 }
2419 } else {
2420 if (maxprotp == NULL)
2421 error = cap_funwrap(fp, needrights, &fp_fromcap);
2422 else
2423 error = cap_funwrap_mmap(fp, needrights, maxprotp,
2424 &fp_fromcap);
2425 if (error != 0) {
2426 fdrop(fp, td);
2427 return (error);
2428 }
2429
2430 /*
2431 * If we've unwrapped a file, drop the original capability
2432 * and hold the new descriptor. fp after this point refers to
2433 * the actual (unwrapped) object, not the capability.
2434 */
2435 if (fp != fp_fromcap) {
2436 fhold(fp_fromcap);
2437 fdrop(fp, td);
2438 fp = fp_fromcap;
2439 }
2440 }
2441 #else /* !CAPABILITIES */
2442 KASSERT(fp->f_type != DTYPE_CAPABILITY,
2443 ("%s: saw capability", __func__));
2444 if (maxprotp != NULL)
2445 *maxprotp = VM_PROT_ALL;
2446 #endif /* CAPABILITIES */
2447
2448 /*
2449 * FREAD and FWRITE failure return EBADF as per POSIX.
2450 */
2451 error = 0;
2452 switch (flags) {
2453 case FREAD:
2454 case FWRITE:
2455 if ((fp->f_flag & flags) == 0)
2456 error = EBADF;
2457 break;
2458 case FEXEC:
2459 if ((fp->f_flag & (FREAD | FEXEC)) == 0 ||
2460 ((fp->f_flag & FWRITE) != 0))
2461 error = EBADF;
2462 break;
2463 case 0:
2464 break;
2465 default:
2466 KASSERT(0, ("wrong flags"));
2467 }
2468
2469 if (error != 0) {
2470 fdrop(fp, td);
2471 return (error);
2472 }
2473
2474 *fpp = fp;
2475 return (0);
2476 }
2477
2478 int
2479 fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2480 {
2481
2482 return(_fget(td, fd, fpp, 0, rights, NULL, NULL, 0));
2483 }
2484
2485 int
2486 fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp,
2487 struct file **fpp)
2488 {
2489
2490 return (_fget(td, fd, fpp, 0, rights, NULL, maxprotp, 0));
2491 }
2492
2493 int
2494 fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2495 {
2496
2497 return(_fget(td, fd, fpp, FREAD, rights, NULL, NULL, 0));
2498 }
2499
2500 int
2501 fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2502 {
2503
2504 return (_fget(td, fd, fpp, FWRITE, rights, NULL, NULL, 0));
2505 }
2506
2507 /*
2508 * Unlike the other fget() calls, which accept and check capability rights
2509 * but never return capabilities, fgetcap() returns the capability but doesn't
2510 * check capability rights.
2511 */
2512 int
2513 fgetcap(struct thread *td, int fd, struct file **fpp)
2514 {
2515
2516 return (_fget(td, fd, fpp, 0, 0, NULL, NULL, FGET_GETCAP));
2517 }
2518
2519
2520 /*
2521 * Like fget() but loads the underlying vnode, or returns an error if the
2522 * descriptor does not represent a vnode. Note that pipes use vnodes but
2523 * never have VM objects. The returned vnode will be vref()'d.
2524 *
2525 * XXX: what about the unused flags ?
2526 */
2527 static __inline int
2528 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights,
2529 cap_rights_t *haverightsp, struct vnode **vpp)
2530 {
2531 struct file *fp;
2532 int error;
2533
2534 *vpp = NULL;
2535 if ((error = _fget(td, fd, &fp, flags, needrights, haverightsp,
2536 NULL, 0)) != 0)
2537 return (error);
2538 if (fp->f_vnode == NULL) {
2539 error = EINVAL;
2540 } else {
2541 *vpp = fp->f_vnode;
2542 vref(*vpp);
2543 }
2544 fdrop(fp, td);
2545
2546 return (error);
2547 }
2548
2549 int
2550 fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2551 {
2552
2553 return (_fgetvp(td, fd, 0, rights, NULL, vpp));
2554 }
2555
2556 int
2557 fgetvp_rights(struct thread *td, int fd, cap_rights_t need, cap_rights_t *have,
2558 struct vnode **vpp)
2559 {
2560 return (_fgetvp(td, fd, 0, need, have, vpp));
2561 }
2562
2563 int
2564 fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2565 {
2566
2567 return (_fgetvp(td, fd, FREAD, rights, NULL, vpp));
2568 }
2569
2570 int
2571 fgetvp_exec(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2572 {
2573
2574 return (_fgetvp(td, fd, FEXEC, rights, NULL, vpp));
2575 }
2576
2577 #ifdef notyet
2578 int
2579 fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
2580 struct vnode **vpp)
2581 {
2582
2583 return (_fgetvp(td, fd, FWRITE, rights, NULL, vpp));
2584 }
2585 #endif
2586
2587 /*
2588 * Like fget() but loads the underlying socket, or returns an error if the
2589 * descriptor does not represent a socket.
2590 *
2591 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2592 * in the future.
2593 *
2594 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2595 * on their file descriptor reference to prevent the socket from being free'd
2596 * during use.
2597 */
2598 int
2599 fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp,
2600 u_int *fflagp)
2601 {
2602 struct file *fp;
2603 int error;
2604
2605 *spp = NULL;
2606 if (fflagp != NULL)
2607 *fflagp = 0;
2608 if ((error = _fget(td, fd, &fp, 0, rights, NULL, NULL, 0)) != 0)
2609 return (error);
2610 if (fp->f_type != DTYPE_SOCKET) {
2611 error = ENOTSOCK;
2612 } else {
2613 *spp = fp->f_data;
2614 if (fflagp)
2615 *fflagp = fp->f_flag;
2616 SOCK_LOCK(*spp);
2617 soref(*spp);
2618 SOCK_UNLOCK(*spp);
2619 }
2620 fdrop(fp, td);
2621
2622 return (error);
2623 }
2624
2625 /*
2626 * Drop the reference count on the socket and XXX release the SX lock in the
2627 * future. The last reference closes the socket.
2628 *
2629 * Note: fputsock() is deprecated, see comment for fgetsock().
2630 */
2631 void
2632 fputsock(struct socket *so)
2633 {
2634
2635 ACCEPT_LOCK();
2636 SOCK_LOCK(so);
2637 CURVNET_SET(so->so_vnet);
2638 sorele(so);
2639 CURVNET_RESTORE();
2640 }
2641
2642 /*
2643 * Handle the last reference to a file being closed.
2644 *
2645 * No special capability handling here, as the capability's fo_close will run
2646 * instead of the object here, and perform any necessary drop on the object.
2647 */
2648 int
2649 _fdrop(struct file *fp, struct thread *td)
2650 {
2651 int error;
2652
2653 error = 0;
2654 if (fp->f_count != 0)
2655 panic("fdrop: count %d", fp->f_count);
2656 if (fp->f_ops != &badfileops)
2657 error = fo_close(fp, td);
2658 atomic_subtract_int(&openfiles, 1);
2659 crfree(fp->f_cred);
2660 free(fp->f_advice, M_FADVISE);
2661 uma_zfree(file_zone, fp);
2662
2663 return (error);
2664 }
2665
2666 /*
2667 * Apply an advisory lock on a file descriptor.
2668 *
2669 * Just attempt to get a record lock of the requested type on the entire file
2670 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2671 */
2672 #ifndef _SYS_SYSPROTO_H_
2673 struct flock_args {
2674 int fd;
2675 int how;
2676 };
2677 #endif
2678 /* ARGSUSED */
2679 int
2680 sys_flock(struct thread *td, struct flock_args *uap)
2681 {
2682 struct file *fp;
2683 struct vnode *vp;
2684 struct flock lf;
2685 int vfslocked;
2686 int error;
2687
2688 if ((error = fget(td, uap->fd, CAP_FLOCK, &fp)) != 0)
2689 return (error);
2690 if (fp->f_type != DTYPE_VNODE) {
2691 fdrop(fp, td);
2692 return (EOPNOTSUPP);
2693 }
2694
2695 vp = fp->f_vnode;
2696 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2697 lf.l_whence = SEEK_SET;
2698 lf.l_start = 0;
2699 lf.l_len = 0;
2700 if (uap->how & LOCK_UN) {
2701 lf.l_type = F_UNLCK;
2702 atomic_clear_int(&fp->f_flag, FHASLOCK);
2703 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2704 goto done2;
2705 }
2706 if (uap->how & LOCK_EX)
2707 lf.l_type = F_WRLCK;
2708 else if (uap->how & LOCK_SH)
2709 lf.l_type = F_RDLCK;
2710 else {
2711 error = EBADF;
2712 goto done2;
2713 }
2714 atomic_set_int(&fp->f_flag, FHASLOCK);
2715 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2716 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2717 done2:
2718 fdrop(fp, td);
2719 VFS_UNLOCK_GIANT(vfslocked);
2720 return (error);
2721 }
2722 /*
2723 * Duplicate the specified descriptor to a free descriptor.
2724 */
2725 int
2726 dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
2727 {
2728 struct file *wfp;
2729 struct file *fp;
2730
2731 /*
2732 * If the to-be-dup'd fd number is greater than the allowed number
2733 * of file descriptors, or the fd to be dup'd has already been
2734 * closed, then reject.
2735 */
2736 FILEDESC_XLOCK(fdp);
2737 if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2738 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2739 FILEDESC_XUNLOCK(fdp);
2740 return (EBADF);
2741 }
2742
2743 /*
2744 * There are two cases of interest here.
2745 *
2746 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2747 *
2748 * For ENXIO steal away the file structure from (dfd) and store it in
2749 * (indx). (dfd) is effectively closed by this operation.
2750 *
2751 * Any other error code is just returned.
2752 */
2753 switch (error) {
2754 case ENODEV:
2755 /*
2756 * Check that the mode the file is being opened for is a
2757 * subset of the mode of the existing descriptor.
2758 */
2759 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2760 FILEDESC_XUNLOCK(fdp);
2761 return (EACCES);
2762 }
2763 fp = fdp->fd_ofiles[indx];
2764 fdp->fd_ofiles[indx] = wfp;
2765 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2766 if (fp == NULL)
2767 fdused(fdp, indx);
2768 fhold(wfp);
2769 FILEDESC_XUNLOCK(fdp);
2770 if (fp != NULL)
2771 /*
2772 * We now own the reference to fp that the ofiles[]
2773 * array used to own. Release it.
2774 */
2775 fdrop(fp, td);
2776 return (0);
2777
2778 case ENXIO:
2779 /*
2780 * Steal away the file pointer from dfd and stuff it into indx.
2781 */
2782 fp = fdp->fd_ofiles[indx];
2783 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2784 fdp->fd_ofiles[dfd] = NULL;
2785 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2786 fdp->fd_ofileflags[dfd] = 0;
2787 fdunused(fdp, dfd);
2788 if (fp == NULL)
2789 fdused(fdp, indx);
2790 FILEDESC_XUNLOCK(fdp);
2791
2792 /*
2793 * We now own the reference to fp that the ofiles[] array
2794 * used to own. Release it.
2795 */
2796 if (fp != NULL)
2797 fdrop(fp, td);
2798 return (0);
2799
2800 default:
2801 FILEDESC_XUNLOCK(fdp);
2802 return (error);
2803 }
2804 /* NOTREACHED */
2805 }
2806
2807 /*
2808 * Scan all active processes and prisons to see if any of them have a current
2809 * or root directory of `olddp'. If so, replace them with the new mount point.
2810 */
2811 void
2812 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2813 {
2814 struct filedesc *fdp;
2815 struct prison *pr;
2816 struct proc *p;
2817 int nrele;
2818
2819 if (vrefcnt(olddp) == 1)
2820 return;
2821 nrele = 0;
2822 sx_slock(&allproc_lock);
2823 FOREACH_PROC_IN_SYSTEM(p) {
2824 fdp = fdhold(p);
2825 if (fdp == NULL)
2826 continue;
2827 FILEDESC_XLOCK(fdp);
2828 if (fdp->fd_cdir == olddp) {
2829 vref(newdp);
2830 fdp->fd_cdir = newdp;
2831 nrele++;
2832 }
2833 if (fdp->fd_rdir == olddp) {
2834 vref(newdp);
2835 fdp->fd_rdir = newdp;
2836 nrele++;
2837 }
2838 if (fdp->fd_jdir == olddp) {
2839 vref(newdp);
2840 fdp->fd_jdir = newdp;
2841 nrele++;
2842 }
2843 FILEDESC_XUNLOCK(fdp);
2844 fddrop(fdp);
2845 }
2846 sx_sunlock(&allproc_lock);
2847 if (rootvnode == olddp) {
2848 vref(newdp);
2849 rootvnode = newdp;
2850 nrele++;
2851 }
2852 mtx_lock(&prison0.pr_mtx);
2853 if (prison0.pr_root == olddp) {
2854 vref(newdp);
2855 prison0.pr_root = newdp;
2856 nrele++;
2857 }
2858 mtx_unlock(&prison0.pr_mtx);
2859 sx_slock(&allprison_lock);
2860 TAILQ_FOREACH(pr, &allprison, pr_list) {
2861 mtx_lock(&pr->pr_mtx);
2862 if (pr->pr_root == olddp) {
2863 vref(newdp);
2864 pr->pr_root = newdp;
2865 nrele++;
2866 }
2867 mtx_unlock(&pr->pr_mtx);
2868 }
2869 sx_sunlock(&allprison_lock);
2870 while (nrele--)
2871 vrele(olddp);
2872 }
2873
2874 struct filedesc_to_leader *
2875 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2876 {
2877 struct filedesc_to_leader *fdtol;
2878
2879 fdtol = malloc(sizeof(struct filedesc_to_leader),
2880 M_FILEDESC_TO_LEADER,
2881 M_WAITOK);
2882 fdtol->fdl_refcount = 1;
2883 fdtol->fdl_holdcount = 0;
2884 fdtol->fdl_wakeup = 0;
2885 fdtol->fdl_leader = leader;
2886 if (old != NULL) {
2887 FILEDESC_XLOCK(fdp);
2888 fdtol->fdl_next = old->fdl_next;
2889 fdtol->fdl_prev = old;
2890 old->fdl_next = fdtol;
2891 fdtol->fdl_next->fdl_prev = fdtol;
2892 FILEDESC_XUNLOCK(fdp);
2893 } else {
2894 fdtol->fdl_next = fdtol;
2895 fdtol->fdl_prev = fdtol;
2896 }
2897 return (fdtol);
2898 }
2899
2900 /*
2901 * Get file structures globally.
2902 */
2903 static int
2904 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2905 {
2906 struct xfile xf;
2907 struct filedesc *fdp;
2908 struct file *fp;
2909 struct proc *p;
2910 int error, n;
2911
2912 error = sysctl_wire_old_buffer(req, 0);
2913 if (error != 0)
2914 return (error);
2915 if (req->oldptr == NULL) {
2916 n = 0;
2917 sx_slock(&allproc_lock);
2918 FOREACH_PROC_IN_SYSTEM(p) {
2919 if (p->p_state == PRS_NEW)
2920 continue;
2921 fdp = fdhold(p);
2922 if (fdp == NULL)
2923 continue;
2924 /* overestimates sparse tables. */
2925 if (fdp->fd_lastfile > 0)
2926 n += fdp->fd_lastfile;
2927 fddrop(fdp);
2928 }
2929 sx_sunlock(&allproc_lock);
2930 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2931 }
2932 error = 0;
2933 bzero(&xf, sizeof(xf));
2934 xf.xf_size = sizeof(xf);
2935 sx_slock(&allproc_lock);
2936 FOREACH_PROC_IN_SYSTEM(p) {
2937 PROC_LOCK(p);
2938 if (p->p_state == PRS_NEW) {
2939 PROC_UNLOCK(p);
2940 continue;
2941 }
2942 if (p_cansee(req->td, p) != 0) {
2943 PROC_UNLOCK(p);
2944 continue;
2945 }
2946 xf.xf_pid = p->p_pid;
2947 xf.xf_uid = p->p_ucred->cr_uid;
2948 PROC_UNLOCK(p);
2949 fdp = fdhold(p);
2950 if (fdp == NULL)
2951 continue;
2952 FILEDESC_SLOCK(fdp);
2953 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2954 if ((fp = fdp->fd_ofiles[n]) == NULL)
2955 continue;
2956 xf.xf_fd = n;
2957 xf.xf_file = fp;
2958 xf.xf_data = fp->f_data;
2959 xf.xf_vnode = fp->f_vnode;
2960 xf.xf_type = fp->f_type;
2961 xf.xf_count = fp->f_count;
2962 xf.xf_msgcount = 0;
2963 xf.xf_offset = foffset_get(fp);
2964 xf.xf_flag = fp->f_flag;
2965 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2966 if (error)
2967 break;
2968 }
2969 FILEDESC_SUNLOCK(fdp);
2970 fddrop(fdp);
2971 if (error)
2972 break;
2973 }
2974 sx_sunlock(&allproc_lock);
2975 return (error);
2976 }
2977
2978 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2979 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2980
2981 #ifdef KINFO_OFILE_SIZE
2982 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2983 #endif
2984
2985 #ifdef COMPAT_FREEBSD7
2986 static int
2987 export_vnode_for_osysctl(struct vnode *vp, int type,
2988 struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2989 {
2990 int error;
2991 char *fullpath, *freepath;
2992 int vfslocked;
2993
2994 bzero(kif, sizeof(*kif));
2995 kif->kf_structsize = sizeof(*kif);
2996
2997 vref(vp);
2998 kif->kf_fd = type;
2999 kif->kf_type = KF_TYPE_VNODE;
3000 /* This function only handles directories. */
3001 if (vp->v_type != VDIR) {
3002 vrele(vp);
3003 return (ENOTDIR);
3004 }
3005 kif->kf_vnode_type = KF_VTYPE_VDIR;
3006
3007 /*
3008 * This is not a true file descriptor, so we set a bogus refcount
3009 * and offset to indicate these fields should be ignored.
3010 */
3011 kif->kf_ref_count = -1;
3012 kif->kf_offset = -1;
3013
3014 freepath = NULL;
3015 fullpath = "-";
3016 FILEDESC_SUNLOCK(fdp);
3017 vn_fullpath(curthread, vp, &fullpath, &freepath);
3018 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3019 vrele(vp);
3020 VFS_UNLOCK_GIANT(vfslocked);
3021 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3022 if (freepath != NULL)
3023 free(freepath, M_TEMP);
3024 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3025 FILEDESC_SLOCK(fdp);
3026 return (error);
3027 }
3028
3029 /*
3030 * Get per-process file descriptors for use by procstat(1), et al.
3031 */
3032 static int
3033 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
3034 {
3035 char *fullpath, *freepath;
3036 struct kinfo_ofile *kif;
3037 struct filedesc *fdp;
3038 int error, i, *name;
3039 struct shmfd *shmfd;
3040 struct socket *so;
3041 struct vnode *vp;
3042 struct ksem *ks;
3043 struct file *fp;
3044 struct proc *p;
3045 struct tty *tp;
3046 int vfslocked;
3047
3048 name = (int *)arg1;
3049 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3050 if (error != 0)
3051 return (error);
3052 fdp = fdhold(p);
3053 PROC_UNLOCK(p);
3054 if (fdp == NULL)
3055 return (ENOENT);
3056 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
3057 FILEDESC_SLOCK(fdp);
3058 if (fdp->fd_cdir != NULL)
3059 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
3060 fdp, req);
3061 if (fdp->fd_rdir != NULL)
3062 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
3063 fdp, req);
3064 if (fdp->fd_jdir != NULL)
3065 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
3066 fdp, req);
3067 for (i = 0; i < fdp->fd_nfiles; i++) {
3068 if ((fp = fdp->fd_ofiles[i]) == NULL)
3069 continue;
3070 bzero(kif, sizeof(*kif));
3071 kif->kf_structsize = sizeof(*kif);
3072 ks = NULL;
3073 vp = NULL;
3074 so = NULL;
3075 tp = NULL;
3076 shmfd = NULL;
3077 kif->kf_fd = i;
3078
3079 #ifdef CAPABILITIES
3080 /*
3081 * When reporting a capability, most fields will be from the
3082 * underlying object, but do mark as a capability. With
3083 * ofiledesc, we don't have a field to export the cap_rights_t,
3084 * but we do with the new filedesc.
3085 */
3086 if (fp->f_type == DTYPE_CAPABILITY) {
3087 kif->kf_flags |= KF_FLAG_CAPABILITY;
3088 (void)cap_funwrap(fp, 0, &fp);
3089 }
3090 #else
3091 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3092 ("sysctl_kern_proc_ofiledesc: saw capability"));
3093 #endif
3094 switch (fp->f_type) {
3095 case DTYPE_VNODE:
3096 kif->kf_type = KF_TYPE_VNODE;
3097 vp = fp->f_vnode;
3098 break;
3099
3100 case DTYPE_SOCKET:
3101 kif->kf_type = KF_TYPE_SOCKET;
3102 so = fp->f_data;
3103 break;
3104
3105 case DTYPE_PIPE:
3106 kif->kf_type = KF_TYPE_PIPE;
3107 break;
3108
3109 case DTYPE_FIFO:
3110 kif->kf_type = KF_TYPE_FIFO;
3111 vp = fp->f_vnode;
3112 break;
3113
3114 case DTYPE_KQUEUE:
3115 kif->kf_type = KF_TYPE_KQUEUE;
3116 break;
3117
3118 case DTYPE_CRYPTO:
3119 kif->kf_type = KF_TYPE_CRYPTO;
3120 break;
3121
3122 case DTYPE_MQUEUE:
3123 kif->kf_type = KF_TYPE_MQUEUE;
3124 break;
3125
3126 case DTYPE_SHM:
3127 kif->kf_type = KF_TYPE_SHM;
3128 shmfd = fp->f_data;
3129 break;
3130
3131 case DTYPE_SEM:
3132 kif->kf_type = KF_TYPE_SEM;
3133 ks = fp->f_data;
3134 break;
3135
3136 case DTYPE_PTS:
3137 kif->kf_type = KF_TYPE_PTS;
3138 tp = fp->f_data;
3139 break;
3140
3141 #ifdef PROCDESC
3142 case DTYPE_PROCDESC:
3143 kif->kf_type = KF_TYPE_PROCDESC;
3144 break;
3145 #endif
3146
3147 default:
3148 kif->kf_type = KF_TYPE_UNKNOWN;
3149 break;
3150 }
3151 kif->kf_ref_count = fp->f_count;
3152 if (fp->f_flag & FREAD)
3153 kif->kf_flags |= KF_FLAG_READ;
3154 if (fp->f_flag & FWRITE)
3155 kif->kf_flags |= KF_FLAG_WRITE;
3156 if (fp->f_flag & FAPPEND)
3157 kif->kf_flags |= KF_FLAG_APPEND;
3158 if (fp->f_flag & FASYNC)
3159 kif->kf_flags |= KF_FLAG_ASYNC;
3160 if (fp->f_flag & FFSYNC)
3161 kif->kf_flags |= KF_FLAG_FSYNC;
3162 if (fp->f_flag & FNONBLOCK)
3163 kif->kf_flags |= KF_FLAG_NONBLOCK;
3164 if (fp->f_flag & O_DIRECT)
3165 kif->kf_flags |= KF_FLAG_DIRECT;
3166 if (fp->f_flag & FHASLOCK)
3167 kif->kf_flags |= KF_FLAG_HASLOCK;
3168 kif->kf_offset = foffset_get(fp);
3169 if (vp != NULL) {
3170 vref(vp);
3171 switch (vp->v_type) {
3172 case VNON:
3173 kif->kf_vnode_type = KF_VTYPE_VNON;
3174 break;
3175 case VREG:
3176 kif->kf_vnode_type = KF_VTYPE_VREG;
3177 break;
3178 case VDIR:
3179 kif->kf_vnode_type = KF_VTYPE_VDIR;
3180 break;
3181 case VBLK:
3182 kif->kf_vnode_type = KF_VTYPE_VBLK;
3183 break;
3184 case VCHR:
3185 kif->kf_vnode_type = KF_VTYPE_VCHR;
3186 break;
3187 case VLNK:
3188 kif->kf_vnode_type = KF_VTYPE_VLNK;
3189 break;
3190 case VSOCK:
3191 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3192 break;
3193 case VFIFO:
3194 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3195 break;
3196 case VBAD:
3197 kif->kf_vnode_type = KF_VTYPE_VBAD;
3198 break;
3199 default:
3200 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3201 break;
3202 }
3203 /*
3204 * It is OK to drop the filedesc lock here as we will
3205 * re-validate and re-evaluate its properties when
3206 * the loop continues.
3207 */
3208 freepath = NULL;
3209 fullpath = "-";
3210 FILEDESC_SUNLOCK(fdp);
3211 vn_fullpath(curthread, vp, &fullpath, &freepath);
3212 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3213 vrele(vp);
3214 VFS_UNLOCK_GIANT(vfslocked);
3215 strlcpy(kif->kf_path, fullpath,
3216 sizeof(kif->kf_path));
3217 if (freepath != NULL)
3218 free(freepath, M_TEMP);
3219 FILEDESC_SLOCK(fdp);
3220 }
3221 if (so != NULL) {
3222 struct sockaddr *sa;
3223
3224 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3225 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3226 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3227 free(sa, M_SONAME);
3228 }
3229 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3230 == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3231 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3232 free(sa, M_SONAME);
3233 }
3234 kif->kf_sock_domain =
3235 so->so_proto->pr_domain->dom_family;
3236 kif->kf_sock_type = so->so_type;
3237 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3238 }
3239 if (tp != NULL) {
3240 strlcpy(kif->kf_path, tty_devname(tp),
3241 sizeof(kif->kf_path));
3242 }
3243 if (shmfd != NULL)
3244 shm_path(shmfd, kif->kf_path, sizeof(kif->kf_path));
3245 if (ks != NULL && ksem_info != NULL)
3246 ksem_info(ks, kif->kf_path, sizeof(kif->kf_path), NULL);
3247 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3248 if (error)
3249 break;
3250 }
3251 FILEDESC_SUNLOCK(fdp);
3252 fddrop(fdp);
3253 free(kif, M_TEMP);
3254 return (0);
3255 }
3256
3257 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3258 sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3259 #endif /* COMPAT_FREEBSD7 */
3260
3261 #ifdef KINFO_FILE_SIZE
3262 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3263 #endif
3264
3265 struct export_fd_buf {
3266 struct filedesc *fdp;
3267 struct sbuf *sb;
3268 ssize_t remainder;
3269 struct kinfo_file kif;
3270 };
3271
3272 static int
3273 export_fd_to_sb(void *data, int type, int fd, int fflags, int refcnt,
3274 int64_t offset, int fd_is_cap, cap_rights_t fd_cap_rights,
3275 struct export_fd_buf *efbuf)
3276 {
3277 struct {
3278 int fflag;
3279 int kf_fflag;
3280 } fflags_table[] = {
3281 { FAPPEND, KF_FLAG_APPEND },
3282 { FASYNC, KF_FLAG_ASYNC },
3283 { FFSYNC, KF_FLAG_FSYNC },
3284 { FHASLOCK, KF_FLAG_HASLOCK },
3285 { FNONBLOCK, KF_FLAG_NONBLOCK },
3286 { FREAD, KF_FLAG_READ },
3287 { FWRITE, KF_FLAG_WRITE },
3288 { O_CREAT, KF_FLAG_CREAT },
3289 { O_DIRECT, KF_FLAG_DIRECT },
3290 { O_EXCL, KF_FLAG_EXCL },
3291 { O_EXEC, KF_FLAG_EXEC },
3292 { O_EXLOCK, KF_FLAG_EXLOCK },
3293 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3294 { O_SHLOCK, KF_FLAG_SHLOCK },
3295 { O_TRUNC, KF_FLAG_TRUNC }
3296 };
3297 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3298 struct kinfo_file *kif;
3299 struct vnode *vp;
3300 int error, locked, vfslocked;
3301 unsigned int i;
3302
3303 if (efbuf->remainder == 0)
3304 return (0);
3305 kif = &efbuf->kif;
3306 bzero(kif, sizeof(*kif));
3307 locked = efbuf->fdp != NULL;
3308 switch (type) {
3309 case KF_TYPE_FIFO:
3310 case KF_TYPE_VNODE:
3311 if (locked) {
3312 FILEDESC_SUNLOCK(efbuf->fdp);
3313 locked = 0;
3314 }
3315 vp = (struct vnode *)data;
3316 error = fill_vnode_info(vp, kif);
3317 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3318 vrele(vp);
3319 VFS_UNLOCK_GIANT(vfslocked);
3320 break;
3321 case KF_TYPE_SOCKET:
3322 error = fill_socket_info((struct socket *)data, kif);
3323 break;
3324 case KF_TYPE_PIPE:
3325 error = fill_pipe_info((struct pipe *)data, kif);
3326 break;
3327 case KF_TYPE_PTS:
3328 error = fill_pts_info((struct tty *)data, kif);
3329 break;
3330 case KF_TYPE_PROCDESC:
3331 error = fill_procdesc_info((struct procdesc *)data, kif);
3332 break;
3333 case KF_TYPE_SEM:
3334 error = fill_sem_info((struct file *)data, kif);
3335 break;
3336 case KF_TYPE_SHM:
3337 error = fill_shm_info((struct file *)data, kif);
3338 break;
3339 default:
3340 error = 0;
3341 }
3342 if (error == 0)
3343 kif->kf_status |= KF_ATTR_VALID;
3344
3345 /*
3346 * Translate file access flags.
3347 */
3348 for (i = 0; i < NFFLAGS; i++)
3349 if (fflags & fflags_table[i].fflag)
3350 kif->kf_flags |= fflags_table[i].kf_fflag;
3351 if (fd_is_cap)
3352 kif->kf_flags |= KF_FLAG_CAPABILITY;
3353 if (fd_is_cap)
3354 kif->kf_cap_rights = fd_cap_rights;
3355 kif->kf_fd = fd;
3356 kif->kf_type = type;
3357 kif->kf_ref_count = refcnt;
3358 kif->kf_offset = offset;
3359 /* Pack record size down */
3360 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3361 strlen(kif->kf_path) + 1;
3362 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3363 if (efbuf->remainder != -1) {
3364 if (efbuf->remainder < kif->kf_structsize) {
3365 /* Terminate export. */
3366 efbuf->remainder = 0;
3367 if (efbuf->fdp != NULL && !locked)
3368 FILEDESC_SLOCK(efbuf->fdp);
3369 return (0);
3370 }
3371 efbuf->remainder -= kif->kf_structsize;
3372 }
3373 if (locked)
3374 FILEDESC_SUNLOCK(efbuf->fdp);
3375 error = sbuf_bcat(efbuf->sb, kif, kif->kf_structsize);
3376 if (efbuf->fdp != NULL)
3377 FILEDESC_SLOCK(efbuf->fdp);
3378 return (error);
3379 }
3380
3381 /*
3382 * Store a process file descriptor information to sbuf.
3383 *
3384 * Takes a locked proc as argument, and returns with the proc unlocked.
3385 */
3386 int
3387 kern_proc_filedesc_out(struct proc *p, struct sbuf *sb, ssize_t maxlen)
3388 {
3389 struct file *fp;
3390 struct filedesc *fdp;
3391 struct export_fd_buf *efbuf;
3392 struct vnode *cttyvp, *textvp, *tracevp;
3393 int64_t offset;
3394 void *data;
3395 int error, i;
3396 int fd_is_cap, type, refcnt, fflags;
3397 cap_rights_t fd_cap_rights;
3398
3399 PROC_LOCK_ASSERT(p, MA_OWNED);
3400
3401 /* ktrace vnode */
3402 tracevp = p->p_tracevp;
3403 if (tracevp != NULL)
3404 vref(tracevp);
3405 /* text vnode */
3406 textvp = p->p_textvp;
3407 if (textvp != NULL)
3408 vref(textvp);
3409 /* Controlling tty. */
3410 cttyvp = NULL;
3411 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3412 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3413 if (cttyvp != NULL)
3414 vref(cttyvp);
3415 }
3416 fdp = fdhold(p);
3417 PROC_UNLOCK(p);
3418 efbuf = malloc(sizeof(*efbuf), M_TEMP, M_WAITOK);
3419 efbuf->fdp = NULL;
3420 efbuf->sb = sb;
3421 efbuf->remainder = maxlen;
3422 if (tracevp != NULL)
3423 export_fd_to_sb(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3424 FREAD | FWRITE, -1, -1, 0, 0, efbuf);
3425 if (textvp != NULL)
3426 export_fd_to_sb(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3427 FREAD, -1, -1, 0, 0, efbuf);
3428 if (cttyvp != NULL)
3429 export_fd_to_sb(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3430 FREAD | FWRITE, -1, -1, 0, 0, efbuf);
3431 error = 0;
3432 if (fdp == NULL)
3433 goto fail;
3434 efbuf->fdp = fdp;
3435 FILEDESC_SLOCK(fdp);
3436 /* working directory */
3437 if (fdp->fd_cdir != NULL) {
3438 vref(fdp->fd_cdir);
3439 data = fdp->fd_cdir;
3440 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3441 FREAD, -1, -1, 0, 0, efbuf);
3442 }
3443 /* root directory */
3444 if (fdp->fd_rdir != NULL) {
3445 vref(fdp->fd_rdir);
3446 data = fdp->fd_rdir;
3447 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3448 FREAD, -1, -1, 0, 0, efbuf);
3449 }
3450 /* jail directory */
3451 if (fdp->fd_jdir != NULL) {
3452 vref(fdp->fd_jdir);
3453 data = fdp->fd_jdir;
3454 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3455 FREAD, -1, -1, 0, 0, efbuf);
3456 }
3457 for (i = 0; i < fdp->fd_nfiles; i++) {
3458 if ((fp = fdp->fd_ofiles[i]) == NULL)
3459 continue;
3460 data = NULL;
3461 fd_is_cap = 0;
3462 fd_cap_rights = 0;
3463
3464 #ifdef CAPABILITIES
3465 /*
3466 * When reporting a capability, most fields will be from the
3467 * underlying object, but do mark as a capability and export
3468 * the capability rights mask.
3469 */
3470 if (fp->f_type == DTYPE_CAPABILITY) {
3471 fd_is_cap = 1;
3472 fd_cap_rights = cap_rights(fp);
3473 (void)cap_funwrap(fp, 0, &fp);
3474 }
3475 #else /* !CAPABILITIES */
3476 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3477 ("sysctl_kern_proc_filedesc: saw capability"));
3478 #endif
3479 switch (fp->f_type) {
3480 case DTYPE_VNODE:
3481 type = KF_TYPE_VNODE;
3482 vref(fp->f_vnode);
3483 data = fp->f_vnode;
3484 break;
3485
3486 case DTYPE_SOCKET:
3487 type = KF_TYPE_SOCKET;
3488 data = fp->f_data;
3489 break;
3490
3491 case DTYPE_PIPE:
3492 type = KF_TYPE_PIPE;
3493 data = fp->f_data;
3494 break;
3495
3496 case DTYPE_FIFO:
3497 type = KF_TYPE_FIFO;
3498 vref(fp->f_vnode);
3499 data = fp->f_vnode;
3500 break;
3501
3502 case DTYPE_KQUEUE:
3503 type = KF_TYPE_KQUEUE;
3504 break;
3505
3506 case DTYPE_CRYPTO:
3507 type = KF_TYPE_CRYPTO;
3508 break;
3509
3510 case DTYPE_MQUEUE:
3511 type = KF_TYPE_MQUEUE;
3512 break;
3513
3514 case DTYPE_SHM:
3515 type = KF_TYPE_SHM;
3516 data = fp;
3517 break;
3518
3519 case DTYPE_SEM:
3520 type = KF_TYPE_SEM;
3521 data = fp;
3522 break;
3523
3524 case DTYPE_PTS:
3525 type = KF_TYPE_PTS;
3526 data = fp->f_data;
3527 break;
3528
3529 #ifdef PROCDESC
3530 case DTYPE_PROCDESC:
3531 type = KF_TYPE_PROCDESC;
3532 data = fp->f_data;
3533 break;
3534 #endif
3535
3536 default:
3537 type = KF_TYPE_UNKNOWN;
3538 break;
3539 }
3540 refcnt = fp->f_count;
3541 fflags = fp->f_flag;
3542 offset = foffset_get(fp);
3543
3544 /*
3545 * Create sysctl entry.
3546 * It is OK to drop the filedesc lock here as we will
3547 * re-validate and re-evaluate its properties when
3548 * the loop continues.
3549 */
3550 error = export_fd_to_sb(data, type, i, fflags, refcnt,
3551 offset, fd_is_cap, fd_cap_rights, efbuf);
3552 if (error)
3553 break;
3554 }
3555 FILEDESC_SUNLOCK(fdp);
3556 fddrop(fdp);
3557 fail:
3558 free(efbuf, M_TEMP);
3559 return (error);
3560 }
3561
3562 #define FILEDESC_SBUF_SIZE (sizeof(struct kinfo_file) * 5)
3563
3564 /*
3565 * Get per-process file descriptors for use by procstat(1), et al.
3566 */
3567 static int
3568 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3569 {
3570 struct sbuf sb;
3571 struct proc *p;
3572 ssize_t maxlen;
3573 int error, error2, *name;
3574
3575 name = (int *)arg1;
3576
3577 sbuf_new_for_sysctl(&sb, NULL, FILEDESC_SBUF_SIZE, req);
3578 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3579 if (error != 0) {
3580 sbuf_delete(&sb);
3581 return (error);
3582 }
3583 maxlen = req->oldptr != NULL ? req->oldlen : -1;
3584 error = kern_proc_filedesc_out(p, &sb, maxlen);
3585 error2 = sbuf_finish(&sb);
3586 sbuf_delete(&sb);
3587 return (error != 0 ? error : error2);
3588 }
3589
3590 int
3591 vntype_to_kinfo(int vtype)
3592 {
3593 struct {
3594 int vtype;
3595 int kf_vtype;
3596 } vtypes_table[] = {
3597 { VBAD, KF_VTYPE_VBAD },
3598 { VBLK, KF_VTYPE_VBLK },
3599 { VCHR, KF_VTYPE_VCHR },
3600 { VDIR, KF_VTYPE_VDIR },
3601 { VFIFO, KF_VTYPE_VFIFO },
3602 { VLNK, KF_VTYPE_VLNK },
3603 { VNON, KF_VTYPE_VNON },
3604 { VREG, KF_VTYPE_VREG },
3605 { VSOCK, KF_VTYPE_VSOCK }
3606 };
3607 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3608 unsigned int i;
3609
3610 /*
3611 * Perform vtype translation.
3612 */
3613 for (i = 0; i < NVTYPES; i++)
3614 if (vtypes_table[i].vtype == vtype)
3615 break;
3616 if (i < NVTYPES)
3617 return (vtypes_table[i].kf_vtype);
3618
3619 return (KF_VTYPE_UNKNOWN);
3620 }
3621
3622 static int
3623 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3624 {
3625 struct vattr va;
3626 char *fullpath, *freepath;
3627 int error, vfslocked;
3628
3629 if (vp == NULL)
3630 return (1);
3631 kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3632 freepath = NULL;
3633 fullpath = "-";
3634 error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3635 if (error == 0) {
3636 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3637 }
3638 if (freepath != NULL)
3639 free(freepath, M_TEMP);
3640
3641 /*
3642 * Retrieve vnode attributes.
3643 */
3644 va.va_fsid = VNOVAL;
3645 va.va_rdev = NODEV;
3646 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3647 vn_lock(vp, LK_SHARED | LK_RETRY);
3648 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3649 VOP_UNLOCK(vp, 0);
3650 VFS_UNLOCK_GIANT(vfslocked);
3651 if (error != 0)
3652 return (error);
3653 if (va.va_fsid != VNOVAL)
3654 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3655 else
3656 kif->kf_un.kf_file.kf_file_fsid =
3657 vp->v_mount->mnt_stat.f_fsid.val[0];
3658 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3659 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3660 kif->kf_un.kf_file.kf_file_size = va.va_size;
3661 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3662 return (0);
3663 }
3664
3665 static int
3666 fill_socket_info(struct socket *so, struct kinfo_file *kif)
3667 {
3668 struct sockaddr *sa;
3669 struct inpcb *inpcb;
3670 struct unpcb *unpcb;
3671 int error;
3672
3673 if (so == NULL)
3674 return (1);
3675 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3676 kif->kf_sock_type = so->so_type;
3677 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3678 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3679 switch(kif->kf_sock_domain) {
3680 case AF_INET:
3681 case AF_INET6:
3682 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3683 if (so->so_pcb != NULL) {
3684 inpcb = (struct inpcb *)(so->so_pcb);
3685 kif->kf_un.kf_sock.kf_sock_inpcb =
3686 (uintptr_t)inpcb->inp_ppcb;
3687 }
3688 }
3689 break;
3690 case AF_UNIX:
3691 if (so->so_pcb != NULL) {
3692 unpcb = (struct unpcb *)(so->so_pcb);
3693 if (unpcb->unp_conn) {
3694 kif->kf_un.kf_sock.kf_sock_unpconn =
3695 (uintptr_t)unpcb->unp_conn;
3696 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3697 so->so_rcv.sb_state;
3698 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3699 so->so_snd.sb_state;
3700 }
3701 }
3702 break;
3703 }
3704 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3705 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3706 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3707 free(sa, M_SONAME);
3708 }
3709 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3710 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3711 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3712 free(sa, M_SONAME);
3713 }
3714 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3715 sizeof(kif->kf_path));
3716 return (0);
3717 }
3718
3719 static int
3720 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3721 {
3722
3723 if (tp == NULL)
3724 return (1);
3725 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3726 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3727 return (0);
3728 }
3729
3730 static int
3731 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3732 {
3733
3734 if (pi == NULL)
3735 return (1);
3736 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3737 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3738 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3739 return (0);
3740 }
3741
3742 static int
3743 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
3744 {
3745
3746 if (pdp == NULL)
3747 return (1);
3748 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3749 return (0);
3750 }
3751
3752 static int
3753 fill_sem_info(struct file *fp, struct kinfo_file *kif)
3754 {
3755 struct thread *td;
3756 struct stat sb;
3757
3758 td = curthread;
3759 if (fp->f_data == NULL)
3760 return (1);
3761 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3762 return (1);
3763 if (ksem_info == NULL)
3764 return (1);
3765 ksem_info(fp->f_data, kif->kf_path, sizeof(kif->kf_path),
3766 &kif->kf_un.kf_sem.kf_sem_value);
3767 kif->kf_un.kf_sem.kf_sem_mode = sb.st_mode;
3768 return (0);
3769 }
3770
3771 static int
3772 fill_shm_info(struct file *fp, struct kinfo_file *kif)
3773 {
3774 struct thread *td;
3775 struct stat sb;
3776
3777 td = curthread;
3778 if (fp->f_data == NULL)
3779 return (1);
3780 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3781 return (1);
3782 shm_path(fp->f_data, kif->kf_path, sizeof(kif->kf_path));
3783 kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
3784 kif->kf_un.kf_file.kf_file_size = sb.st_size;
3785 return (0);
3786 }
3787
3788 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3789 sysctl_kern_proc_filedesc, "Process filedesc entries");
3790
3791 #ifdef DDB
3792 /*
3793 * For the purposes of debugging, generate a human-readable string for the
3794 * file type.
3795 */
3796 static const char *
3797 file_type_to_name(short type)
3798 {
3799
3800 switch (type) {
3801 case 0:
3802 return ("zero");
3803 case DTYPE_VNODE:
3804 return ("vnod");
3805 case DTYPE_SOCKET:
3806 return ("sock");
3807 case DTYPE_PIPE:
3808 return ("pipe");
3809 case DTYPE_FIFO:
3810 return ("fifo");
3811 case DTYPE_KQUEUE:
3812 return ("kque");
3813 case DTYPE_CRYPTO:
3814 return ("crpt");
3815 case DTYPE_MQUEUE:
3816 return ("mque");
3817 case DTYPE_SHM:
3818 return ("shm");
3819 case DTYPE_SEM:
3820 return ("ksem");
3821 default:
3822 return ("unkn");
3823 }
3824 }
3825
3826 /*
3827 * For the purposes of debugging, identify a process (if any, perhaps one of
3828 * many) that references the passed file in its file descriptor array. Return
3829 * NULL if none.
3830 */
3831 static struct proc *
3832 file_to_first_proc(struct file *fp)
3833 {
3834 struct filedesc *fdp;
3835 struct proc *p;
3836 int n;
3837
3838 FOREACH_PROC_IN_SYSTEM(p) {
3839 if (p->p_state == PRS_NEW)
3840 continue;
3841 fdp = p->p_fd;
3842 if (fdp == NULL)
3843 continue;
3844 for (n = 0; n < fdp->fd_nfiles; n++) {
3845 if (fp == fdp->fd_ofiles[n])
3846 return (p);
3847 }
3848 }
3849 return (NULL);
3850 }
3851
3852 static void
3853 db_print_file(struct file *fp, int header)
3854 {
3855 struct proc *p;
3856
3857 if (header)
3858 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3859 "File", "Type", "Data", "Flag", "GCFl", "Count",
3860 "MCount", "Vnode", "FPID", "FCmd");
3861 p = file_to_first_proc(fp);
3862 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3863 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3864 0, fp->f_count, 0, fp->f_vnode,
3865 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3866 }
3867
3868 DB_SHOW_COMMAND(file, db_show_file)
3869 {
3870 struct file *fp;
3871
3872 if (!have_addr) {
3873 db_printf("usage: show file <addr>\n");
3874 return;
3875 }
3876 fp = (struct file *)addr;
3877 db_print_file(fp, 1);
3878 }
3879
3880 DB_SHOW_COMMAND(files, db_show_files)
3881 {
3882 struct filedesc *fdp;
3883 struct file *fp;
3884 struct proc *p;
3885 int header;
3886 int n;
3887
3888 header = 1;
3889 FOREACH_PROC_IN_SYSTEM(p) {
3890 if (p->p_state == PRS_NEW)
3891 continue;
3892 if ((fdp = p->p_fd) == NULL)
3893 continue;
3894 for (n = 0; n < fdp->fd_nfiles; ++n) {
3895 if ((fp = fdp->fd_ofiles[n]) == NULL)
3896 continue;
3897 db_print_file(fp, header);
3898 header = 0;
3899 }
3900 }
3901 }
3902 #endif
3903
3904 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3905 &maxfilesperproc, 0, "Maximum files allowed open per process");
3906
3907 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3908 &maxfiles, 0, "Maximum number of files");
3909
3910 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3911 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3912
3913 /* ARGSUSED*/
3914 static void
3915 filelistinit(void *dummy)
3916 {
3917
3918 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3919 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3920 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3921 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3922 }
3923 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3924
3925 /*-------------------------------------------------------------------*/
3926
3927 static int
3928 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred,
3929 int flags, struct thread *td)
3930 {
3931
3932 return (EBADF);
3933 }
3934
3935 static int
3936 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
3937 struct thread *td)
3938 {
3939
3940 return (EINVAL);
3941 }
3942
3943 static int
3944 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
3945 struct thread *td)
3946 {
3947
3948 return (EBADF);
3949 }
3950
3951 static int
3952 badfo_poll(struct file *fp, int events, struct ucred *active_cred,
3953 struct thread *td)
3954 {
3955
3956 return (0);
3957 }
3958
3959 static int
3960 badfo_kqfilter(struct file *fp, struct knote *kn)
3961 {
3962
3963 return (EBADF);
3964 }
3965
3966 static int
3967 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
3968 struct thread *td)
3969 {
3970
3971 return (EBADF);
3972 }
3973
3974 static int
3975 badfo_close(struct file *fp, struct thread *td)
3976 {
3977
3978 return (EBADF);
3979 }
3980
3981 static int
3982 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3983 struct thread *td)
3984 {
3985
3986 return (EBADF);
3987 }
3988
3989 static int
3990 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3991 struct thread *td)
3992 {
3993
3994 return (EBADF);
3995 }
3996
3997 struct fileops badfileops = {
3998 .fo_read = badfo_readwrite,
3999 .fo_write = badfo_readwrite,
4000 .fo_truncate = badfo_truncate,
4001 .fo_ioctl = badfo_ioctl,
4002 .fo_poll = badfo_poll,
4003 .fo_kqfilter = badfo_kqfilter,
4004 .fo_stat = badfo_stat,
4005 .fo_close = badfo_close,
4006 .fo_chmod = badfo_chmod,
4007 .fo_chown = badfo_chown,
4008 };
4009
4010 int
4011 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
4012 struct thread *td)
4013 {
4014
4015 return (EINVAL);
4016 }
4017
4018 int
4019 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
4020 struct thread *td)
4021 {
4022
4023 return (EINVAL);
4024 }
4025
4026 /*-------------------------------------------------------------------*/
4027
4028 /*
4029 * File Descriptor pseudo-device driver (/dev/fd/).
4030 *
4031 * Opening minor device N dup()s the file (if any) connected to file
4032 * descriptor N belonging to the calling process. Note that this driver
4033 * consists of only the ``open()'' routine, because all subsequent
4034 * references to this file will be direct to the other driver.
4035 *
4036 * XXX: we could give this one a cloning event handler if necessary.
4037 */
4038
4039 /* ARGSUSED */
4040 static int
4041 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
4042 {
4043
4044 /*
4045 * XXX Kludge: set curthread->td_dupfd to contain the value of the
4046 * the file descriptor being sought for duplication. The error
4047 * return ensures that the vnode for this device will be released
4048 * by vn_open. Open will detect this special error and take the
4049 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
4050 * will simply report the error.
4051 */
4052 td->td_dupfd = dev2unit(dev);
4053 return (ENODEV);
4054 }
4055
4056 static struct cdevsw fildesc_cdevsw = {
4057 .d_version = D_VERSION,
4058 .d_open = fdopen,
4059 .d_name = "FD",
4060 };
4061
4062 static void
4063 fildesc_drvinit(void *unused)
4064 {
4065 struct cdev *dev;
4066
4067 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
4068 UID_ROOT, GID_WHEEL, 0666, "fd/0");
4069 make_dev_alias(dev, "stdin");
4070 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
4071 UID_ROOT, GID_WHEEL, 0666, "fd/1");
4072 make_dev_alias(dev, "stdout");
4073 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
4074 UID_ROOT, GID_WHEEL, 0666, "fd/2");
4075 make_dev_alias(dev, "stderr");
4076 }
4077
4078 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);
Cache object: f1b27c3693541509738683dbabd0caab
|