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