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