]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/filedesc.h
Add new function fdunshare() which encapsulates the necessary light magic
[FreeBSD/FreeBSD.git] / sys / sys / filedesc.h
1 /*
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)filedesc.h  8.1 (Berkeley) 6/2/93
30  * $FreeBSD$
31  */
32
33 #ifndef _SYS_FILEDESC_H_
34 #define _SYS_FILEDESC_H_
35
36 #include <sys/queue.h>
37 #include <sys/event.h>
38 #include <sys/priority.h>
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
41
42 #include <machine/_limits.h>
43
44 /*
45  * This structure is used for the management of descriptors.  It may be
46  * shared by multiple processes.
47  */
48 #define NDSLOTTYPE      u_long
49
50 struct filedesc {
51         struct  file **fd_ofiles;       /* file structures for open files */
52         char    *fd_ofileflags;         /* per-process open file flags */
53         struct  vnode *fd_cdir;         /* current directory */
54         struct  vnode *fd_rdir;         /* root directory */
55         struct  vnode *fd_jdir;         /* jail root directory */
56         int     fd_nfiles;              /* number of open files allocated */
57         NDSLOTTYPE *fd_map;             /* bitmap of free fds */
58         int     fd_lastfile;            /* high-water mark of fd_ofiles */
59         int     fd_freefile;            /* approx. next free file */
60         u_short fd_cmask;               /* mask for file creation */
61         u_short fd_refcnt;              /* reference count */
62
63         struct  mtx fd_mtx;             /* protects members of this struct */
64         int     fd_locked;              /* long lock flag */
65         int     fd_wanted;              /* "" */
66         struct  kqlist fd_kqlist;       /* list of kqueues on this filedesc */
67         int     fd_holdleaderscount;    /* block fdfree() for shared close() */
68         int     fd_holdleaderswakeup;   /* fdfree() needs wakeup */
69 };
70
71 /*
72  * Structure to keep track of (process leader, struct fildedesc) tuples.
73  * Each process has a pointer to such a structure when detailed tracking
74  * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
75  * table to be shared by processes having different "p_leader" pointers
76  * and thus distinct POSIX style locks.
77  *
78  * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
79  */
80 struct filedesc_to_leader {
81         int             fdl_refcount;   /* references from struct proc */
82         int             fdl_holdcount;  /* temporary hold during closef */
83         int             fdl_wakeup;     /* fdfree() waits on closef() */
84         struct proc     *fdl_leader;    /* owner of POSIX locks */
85         /* Circular list: */
86         struct filedesc_to_leader *fdl_prev;
87         struct filedesc_to_leader *fdl_next;
88 };
89
90 /*
91  * Per-process open flags.
92  */
93 #define UF_EXCLOSE      0x01            /* auto-close on exec */
94
95 #ifdef _KERNEL
96
97 /* Lock a file descriptor table. */
98 #define FILEDESC_LOCK(fd)                                                               \
99         do {                                                                            \
100                 mtx_lock(&(fd)->fd_mtx);                                                \
101                 (fd)->fd_wanted++;                                                      \
102                 while ((fd)->fd_locked)                                                 \
103                         msleep(&(fd)->fd_locked, &(fd)->fd_mtx, PLOCK, "fdesc", 0);     \
104                 (fd)->fd_locked = 2;                                                    \
105                 (fd)->fd_wanted--;                                                      \
106                 mtx_unlock(&(fd)->fd_mtx);                                              \
107         } while (0);
108
109 #define FILEDESC_UNLOCK(fd)                                                             \
110         do {                                                                            \
111                 mtx_lock(&(fd)->fd_mtx);                                                \
112                 KASSERT((fd)->fd_locked == 2,                                           \
113                     ("fdesc locking mistake %d should be %d", (fd)->fd_locked, 2));     \
114                 (fd)->fd_locked = 0;                                                    \
115                 if ((fd)->fd_wanted)                                                    \
116                         wakeup(&(fd)->fd_locked);                                       \
117                 mtx_unlock(&(fd)->fd_mtx);                                              \
118         } while (0);
119
120 #define FILEDESC_LOCK_FAST(fd)                                                          \
121         do {                                                                            \
122                 mtx_lock(&(fd)->fd_mtx);                                                \
123                 (fd)->fd_wanted++;                                                      \
124                 while ((fd)->fd_locked)                                                 \
125                         msleep(&(fd)->fd_locked, &(fd)->fd_mtx, PLOCK, "fdesc", 0);     \
126                 (fd)->fd_locked = 1;                                                    \
127                 (fd)->fd_wanted--;                                                      \
128         } while (0);
129
130 #define FILEDESC_UNLOCK_FAST(fd)                                                        \
131         do {                                                                            \
132                 KASSERT((fd)->fd_locked == 1,                                           \
133                     ("fdesc locking mistake %d should be %d", (fd)->fd_locked, 1));     \
134                 (fd)->fd_locked = 0;                                                    \
135                 if ((fd)->fd_wanted)                                                    \
136                         wakeup(&(fd)->fd_locked);                                       \
137                 mtx_unlock(&(fd)->fd_mtx);                                              \
138         } while (0);
139
140 #ifdef INVARIANT_SUPPORT
141 #define FILEDESC_LOCK_ASSERT(fd, arg)                                                   \
142         do {                                                                            \
143                 if ((arg) == MA_OWNED)                                                  \
144                         KASSERT((fd)->fd_locked != 0, ("fdesc locking mistake"));       \
145                 else                                                                    \
146                         KASSERT((fd)->fd_locked == 0, ("fdesc locking mistake"));       \
147         } while (0);
148 #else
149 #define FILEDESC_LOCK_ASSERT(fd, arg)
150 #endif
151
152 #define FILEDESC_LOCK_DESC      "filedesc structure"
153
154 struct thread;
155
156 int     closef(struct file *fp, struct thread *td);
157 int     dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd,
158             int mode, int error);
159 int     falloc(struct thread *td, struct file **resultfp, int *resultfd);
160 int     fdalloc(struct thread *td, int minfd, int *result);
161 int     fdavail(struct thread *td, int n);
162 int     fdcheckstd(struct thread *td);
163 void    fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td);
164 void    fdcloseexec(struct thread *td);
165 struct  filedesc *fdcopy(struct filedesc *fdp);
166 void    fdunshare(struct proc *p, struct thread *td);
167 void    fdfree(struct thread *td);
168 struct  filedesc *fdinit(struct filedesc *fdp);
169 struct  filedesc *fdshare(struct filedesc *fdp);
170 void    fdused(struct filedesc *fdp, int fd);
171 struct filedesc_to_leader *
172         filedesc_to_leader_alloc(struct filedesc_to_leader *old,
173             struct filedesc *fdp, struct proc *leader);
174 int     getvnode(struct filedesc *fdp, int fd, struct file **fpp);
175 void    setugidsafety(struct thread *td);
176
177 static __inline struct file *
178 fget_locked(struct filedesc *fdp, int fd)
179 {
180
181         return (fd < 0 || fd >= fdp->fd_nfiles ? NULL : fdp->fd_ofiles[fd]);
182 }
183
184 extern struct mtx fdesc_mtx;
185
186 #endif /* _KERNEL */
187
188 #endif /* !_SYS_FILEDESC_H_ */