kern_descrip.c revision 1.1.1.2 1 1.1.1.2 fvdl /*
2 1.1.1.2 fvdl * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 1.1.1.2 fvdl * The Regents of the University of California. All rights reserved.
4 1.1.1.2 fvdl * (c) UNIX System Laboratories, Inc.
5 1.1.1.2 fvdl * All or some portions of this file are derived from material licensed
6 1.1.1.2 fvdl * to the University of California by American Telephone and Telegraph
7 1.1.1.2 fvdl * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.1.1.2 fvdl * the permission of UNIX System Laboratories, Inc.
9 1.1.1.2 fvdl *
10 1.1.1.2 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1.1.2 fvdl * modification, are permitted provided that the following conditions
12 1.1.1.2 fvdl * are met:
13 1.1.1.2 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1.1.2 fvdl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer in the
17 1.1.1.2 fvdl * documentation and/or other materials provided with the distribution.
18 1.1.1.2 fvdl * 3. All advertising materials mentioning features or use of this software
19 1.1.1.2 fvdl * must display the following acknowledgement:
20 1.1.1.2 fvdl * This product includes software developed by the University of
21 1.1.1.2 fvdl * California, Berkeley and its contributors.
22 1.1.1.2 fvdl * 4. Neither the name of the University nor the names of its contributors
23 1.1.1.2 fvdl * may be used to endorse or promote products derived from this software
24 1.1.1.2 fvdl * without specific prior written permission.
25 1.1.1.2 fvdl *
26 1.1.1.2 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1.1.2 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1.1.2 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1.1.2 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1.1.2 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1.1.2 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1.1.2 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1.1.2 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1.1.2 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1.1.2 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1.1.2 fvdl * SUCH DAMAGE.
37 1.1.1.2 fvdl *
38 1.1.1.2 fvdl * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
39 1.1.1.2 fvdl */
40 1.1.1.2 fvdl
41 1.1.1.2 fvdl #include <sys/param.h>
42 1.1.1.2 fvdl #include <sys/systm.h>
43 1.1.1.2 fvdl #include <sys/filedesc.h>
44 1.1.1.2 fvdl #include <sys/kernel.h>
45 1.1.1.2 fvdl #include <sys/vnode.h>
46 1.1.1.2 fvdl #include <sys/proc.h>
47 1.1.1.2 fvdl #include <sys/file.h>
48 1.1.1.2 fvdl #include <sys/socket.h>
49 1.1.1.2 fvdl #include <sys/socketvar.h>
50 1.1.1.2 fvdl #include <sys/stat.h>
51 1.1.1.2 fvdl #include <sys/ioctl.h>
52 1.1.1.2 fvdl #include <sys/fcntl.h>
53 1.1.1.2 fvdl #include <sys/malloc.h>
54 1.1.1.2 fvdl #include <sys/syslog.h>
55 1.1.1.2 fvdl #include <sys/unistd.h>
56 1.1.1.2 fvdl #include <sys/resourcevar.h>
57 1.1.1.2 fvdl
58 1.1.1.2 fvdl /*
59 1.1.1.2 fvdl * Descriptor management.
60 1.1.1.2 fvdl */
61 1.1.1.2 fvdl struct file *filehead; /* head of list of open files */
62 1.1.1.2 fvdl int nfiles; /* actual number of open files */
63 1.1.1.2 fvdl
64 1.1.1.2 fvdl /*
65 1.1.1.2 fvdl * System calls on descriptors.
66 1.1.1.2 fvdl */
67 1.1.1.2 fvdl struct getdtablesize_args {
68 1.1.1.2 fvdl int dummy;
69 1.1.1.2 fvdl };
70 1.1.1.2 fvdl /* ARGSUSED */
71 1.1.1.2 fvdl getdtablesize(p, uap, retval)
72 1.1.1.2 fvdl struct proc *p;
73 1.1.1.2 fvdl struct getdtablesize_args *uap;
74 1.1.1.2 fvdl int *retval;
75 1.1.1.2 fvdl {
76 1.1.1.2 fvdl
77 1.1.1.2 fvdl *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
78 1.1.1.2 fvdl return (0);
79 1.1.1.2 fvdl }
80 1.1.1.2 fvdl
81 1.1.1.2 fvdl /*
82 1.1.1.2 fvdl * Duplicate a file descriptor.
83 1.1.1.2 fvdl */
84 1.1.1.2 fvdl struct dup_args {
85 1.1.1.2 fvdl u_int fd;
86 1.1.1.2 fvdl };
87 1.1.1.2 fvdl /* ARGSUSED */
88 1.1.1.2 fvdl dup(p, uap, retval)
89 1.1.1.2 fvdl struct proc *p;
90 1.1.1.2 fvdl struct dup_args *uap;
91 1.1.1.2 fvdl int *retval;
92 1.1.1.2 fvdl {
93 1.1.1.2 fvdl register struct filedesc *fdp;
94 1.1.1.2 fvdl u_int old;
95 1.1.1.2 fvdl int new, error;
96 1.1.1.2 fvdl
97 1.1.1.2 fvdl old = uap->fd;
98 1.1.1.2 fvdl /*
99 1.1.1.2 fvdl * XXX Compatibility
100 1.1.1.2 fvdl */
101 1.1.1.2 fvdl if (old &~ 077) { uap->fd &= 077; return (dup2(p, uap, retval)); }
102 1.1.1.2 fvdl
103 1.1.1.2 fvdl fdp = p->p_fd;
104 1.1.1.2 fvdl if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
105 1.1.1.2 fvdl return (EBADF);
106 1.1.1.2 fvdl if (error = fdalloc(p, 0, &new))
107 1.1.1.2 fvdl return (error);
108 1.1.1.2 fvdl return (finishdup(fdp, (int)old, new, retval));
109 1.1.1.2 fvdl }
110 1.1.1.2 fvdl
111 1.1.1.2 fvdl /*
112 1.1.1.2 fvdl * Duplicate a file descriptor to a particular value.
113 1.1.1.2 fvdl */
114 1.1.1.2 fvdl struct dup2_args {
115 1.1.1.2 fvdl u_int from;
116 1.1.1.2 fvdl u_int to;
117 1.1.1.2 fvdl };
118 1.1.1.2 fvdl /* ARGSUSED */
119 1.1.1.2 fvdl dup2(p, uap, retval)
120 1.1.1.2 fvdl struct proc *p;
121 1.1.1.2 fvdl struct dup2_args *uap;
122 1.1.1.2 fvdl int *retval;
123 1.1.1.2 fvdl {
124 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
125 1.1.1.2 fvdl register u_int old = uap->from, new = uap->to;
126 1.1.1.2 fvdl int i, error;
127 1.1.1.2 fvdl
128 1.1.1.2 fvdl if (old >= fdp->fd_nfiles ||
129 1.1.1.2 fvdl fdp->fd_ofiles[old] == NULL ||
130 1.1.1.2 fvdl new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
131 1.1.1.2 fvdl new >= maxfiles)
132 1.1.1.2 fvdl return (EBADF);
133 1.1.1.2 fvdl if (old == new) {
134 1.1.1.2 fvdl *retval = new;
135 1.1.1.2 fvdl return (0);
136 1.1.1.2 fvdl }
137 1.1.1.2 fvdl if (new >= fdp->fd_nfiles) {
138 1.1.1.2 fvdl if (error = fdalloc(p, new, &i))
139 1.1.1.2 fvdl return (error);
140 1.1.1.2 fvdl if (new != i)
141 1.1.1.2 fvdl panic("dup2: fdalloc");
142 1.1.1.2 fvdl } else if (fdp->fd_ofiles[new]) {
143 1.1.1.2 fvdl if (fdp->fd_ofileflags[new] & UF_MAPPED)
144 1.1.1.2 fvdl (void) munmapfd(p, new);
145 1.1.1.2 fvdl /*
146 1.1.1.2 fvdl * dup2() must succeed even if the close has an error.
147 1.1.1.2 fvdl */
148 1.1.1.2 fvdl (void) closef(fdp->fd_ofiles[new], p);
149 1.1.1.2 fvdl }
150 1.1.1.2 fvdl return (finishdup(fdp, (int)old, (int)new, retval));
151 1.1.1.2 fvdl }
152 1.1.1.2 fvdl
153 1.1.1.2 fvdl /*
154 1.1.1.2 fvdl * The file control system call.
155 1.1.1.2 fvdl */
156 1.1.1.2 fvdl struct fcntl_args {
157 1.1.1.2 fvdl int fd;
158 1.1.1.2 fvdl int cmd;
159 1.1.1.2 fvdl int arg;
160 1.1.1.2 fvdl };
161 1.1.1.2 fvdl /* ARGSUSED */
162 1.1.1.2 fvdl fcntl(p, uap, retval)
163 1.1.1.2 fvdl struct proc *p;
164 1.1.1.2 fvdl register struct fcntl_args *uap;
165 1.1.1.2 fvdl int *retval;
166 1.1.1.2 fvdl {
167 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
168 1.1.1.2 fvdl register struct file *fp;
169 1.1.1.2 fvdl register char *pop;
170 1.1.1.2 fvdl struct vnode *vp;
171 1.1.1.2 fvdl int i, tmp, error, flg = F_POSIX;
172 1.1.1.2 fvdl struct flock fl;
173 1.1.1.2 fvdl u_int newmin;
174 1.1.1.2 fvdl
175 1.1.1.2 fvdl if ((unsigned)uap->fd >= fdp->fd_nfiles ||
176 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL)
177 1.1.1.2 fvdl return (EBADF);
178 1.1.1.2 fvdl pop = &fdp->fd_ofileflags[uap->fd];
179 1.1.1.2 fvdl switch (uap->cmd) {
180 1.1.1.2 fvdl
181 1.1.1.2 fvdl case F_DUPFD:
182 1.1.1.2 fvdl newmin = uap->arg;
183 1.1.1.2 fvdl if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
184 1.1.1.2 fvdl newmin >= maxfiles)
185 1.1.1.2 fvdl return (EINVAL);
186 1.1.1.2 fvdl if (error = fdalloc(p, newmin, &i))
187 1.1.1.2 fvdl return (error);
188 1.1.1.2 fvdl return (finishdup(fdp, uap->fd, i, retval));
189 1.1.1.2 fvdl
190 1.1.1.2 fvdl case F_GETFD:
191 1.1.1.2 fvdl *retval = *pop & 1;
192 1.1.1.2 fvdl return (0);
193 1.1.1.2 fvdl
194 1.1.1.2 fvdl case F_SETFD:
195 1.1.1.2 fvdl *pop = (*pop &~ 1) | (uap->arg & 1);
196 1.1.1.2 fvdl return (0);
197 1.1.1.2 fvdl
198 1.1.1.2 fvdl case F_GETFL:
199 1.1.1.2 fvdl *retval = OFLAGS(fp->f_flag);
200 1.1.1.2 fvdl return (0);
201 1.1.1.2 fvdl
202 1.1.1.2 fvdl case F_SETFL:
203 1.1.1.2 fvdl fp->f_flag &= ~FCNTLFLAGS;
204 1.1.1.2 fvdl fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
205 1.1.1.2 fvdl tmp = fp->f_flag & FNONBLOCK;
206 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
207 1.1.1.2 fvdl if (error)
208 1.1.1.2 fvdl return (error);
209 1.1.1.2 fvdl tmp = fp->f_flag & FASYNC;
210 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
211 1.1.1.2 fvdl if (!error)
212 1.1.1.2 fvdl return (0);
213 1.1.1.2 fvdl fp->f_flag &= ~FNONBLOCK;
214 1.1.1.2 fvdl tmp = 0;
215 1.1.1.2 fvdl (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
216 1.1.1.2 fvdl return (error);
217 1.1.1.2 fvdl
218 1.1.1.2 fvdl case F_GETOWN:
219 1.1.1.2 fvdl if (fp->f_type == DTYPE_SOCKET) {
220 1.1.1.2 fvdl *retval = ((struct socket *)fp->f_data)->so_pgid;
221 1.1.1.2 fvdl return (0);
222 1.1.1.2 fvdl }
223 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)
224 1.1.1.2 fvdl (fp, (int)TIOCGPGRP, (caddr_t)retval, p);
225 1.1.1.2 fvdl *retval = -*retval;
226 1.1.1.2 fvdl return (error);
227 1.1.1.2 fvdl
228 1.1.1.2 fvdl case F_SETOWN:
229 1.1.1.2 fvdl if (fp->f_type == DTYPE_SOCKET) {
230 1.1.1.2 fvdl ((struct socket *)fp->f_data)->so_pgid = uap->arg;
231 1.1.1.2 fvdl return (0);
232 1.1.1.2 fvdl }
233 1.1.1.2 fvdl if (uap->arg <= 0) {
234 1.1.1.2 fvdl uap->arg = -uap->arg;
235 1.1.1.2 fvdl } else {
236 1.1.1.2 fvdl struct proc *p1 = pfind(uap->arg);
237 1.1.1.2 fvdl if (p1 == 0)
238 1.1.1.2 fvdl return (ESRCH);
239 1.1.1.2 fvdl uap->arg = p1->p_pgrp->pg_id;
240 1.1.1.2 fvdl }
241 1.1.1.2 fvdl return ((*fp->f_ops->fo_ioctl)
242 1.1.1.2 fvdl (fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
243 1.1.1.2 fvdl
244 1.1.1.2 fvdl case F_SETLKW:
245 1.1.1.2 fvdl flg |= F_WAIT;
246 1.1.1.2 fvdl /* Fall into F_SETLK */
247 1.1.1.2 fvdl
248 1.1.1.2 fvdl case F_SETLK:
249 1.1.1.2 fvdl if (fp->f_type != DTYPE_VNODE)
250 1.1.1.2 fvdl return (EBADF);
251 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
252 1.1.1.2 fvdl /* Copy in the lock structure */
253 1.1.1.2 fvdl error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
254 1.1.1.2 fvdl if (error)
255 1.1.1.2 fvdl return (error);
256 1.1.1.2 fvdl if (fl.l_whence == SEEK_CUR)
257 1.1.1.2 fvdl fl.l_start += fp->f_offset;
258 1.1.1.2 fvdl switch (fl.l_type) {
259 1.1.1.2 fvdl
260 1.1.1.2 fvdl case F_RDLCK:
261 1.1.1.2 fvdl if ((fp->f_flag & FREAD) == 0)
262 1.1.1.2 fvdl return (EBADF);
263 1.1.1.2 fvdl p->p_flag |= P_ADVLOCK;
264 1.1.1.2 fvdl return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
265 1.1.1.2 fvdl
266 1.1.1.2 fvdl case F_WRLCK:
267 1.1.1.2 fvdl if ((fp->f_flag & FWRITE) == 0)
268 1.1.1.2 fvdl return (EBADF);
269 1.1.1.2 fvdl p->p_flag |= P_ADVLOCK;
270 1.1.1.2 fvdl return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
271 1.1.1.2 fvdl
272 1.1.1.2 fvdl case F_UNLCK:
273 1.1.1.2 fvdl return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
274 1.1.1.2 fvdl F_POSIX));
275 1.1.1.2 fvdl
276 1.1.1.2 fvdl default:
277 1.1.1.2 fvdl return (EINVAL);
278 1.1.1.2 fvdl }
279 1.1.1.2 fvdl
280 1.1.1.2 fvdl case F_GETLK:
281 1.1.1.2 fvdl if (fp->f_type != DTYPE_VNODE)
282 1.1.1.2 fvdl return (EBADF);
283 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
284 1.1.1.2 fvdl /* Copy in the lock structure */
285 1.1.1.2 fvdl error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
286 1.1.1.2 fvdl if (error)
287 1.1.1.2 fvdl return (error);
288 1.1.1.2 fvdl if (fl.l_whence == SEEK_CUR)
289 1.1.1.2 fvdl fl.l_start += fp->f_offset;
290 1.1.1.2 fvdl if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
291 1.1.1.2 fvdl return (error);
292 1.1.1.2 fvdl return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl)));
293 1.1.1.2 fvdl
294 1.1.1.2 fvdl default:
295 1.1.1.2 fvdl return (EINVAL);
296 1.1.1.2 fvdl }
297 1.1.1.2 fvdl /* NOTREACHED */
298 1.1.1.2 fvdl }
299 1.1.1.2 fvdl
300 1.1.1.2 fvdl /*
301 1.1.1.2 fvdl * Common code for dup, dup2, and fcntl(F_DUPFD).
302 1.1.1.2 fvdl */
303 1.1.1.2 fvdl int
304 1.1.1.2 fvdl finishdup(fdp, old, new, retval)
305 1.1.1.2 fvdl register struct filedesc *fdp;
306 1.1.1.2 fvdl register int old, new, *retval;
307 1.1.1.2 fvdl {
308 1.1.1.2 fvdl register struct file *fp;
309 1.1.1.2 fvdl
310 1.1.1.2 fvdl fp = fdp->fd_ofiles[old];
311 1.1.1.2 fvdl fdp->fd_ofiles[new] = fp;
312 1.1.1.2 fvdl fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
313 1.1.1.2 fvdl fp->f_count++;
314 1.1.1.2 fvdl if (new > fdp->fd_lastfile)
315 1.1.1.2 fvdl fdp->fd_lastfile = new;
316 1.1.1.2 fvdl *retval = new;
317 1.1.1.2 fvdl return (0);
318 1.1.1.2 fvdl }
319 1.1.1.2 fvdl
320 1.1.1.2 fvdl /*
321 1.1.1.2 fvdl * Close a file descriptor.
322 1.1.1.2 fvdl */
323 1.1.1.2 fvdl struct close_args {
324 1.1.1.2 fvdl int fd;
325 1.1.1.2 fvdl };
326 1.1.1.2 fvdl /* ARGSUSED */
327 1.1.1.2 fvdl close(p, uap, retval)
328 1.1.1.2 fvdl struct proc *p;
329 1.1.1.2 fvdl struct close_args *uap;
330 1.1.1.2 fvdl int *retval;
331 1.1.1.2 fvdl {
332 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
333 1.1.1.2 fvdl register struct file *fp;
334 1.1.1.2 fvdl register int fd = uap->fd;
335 1.1.1.2 fvdl register u_char *pf;
336 1.1.1.2 fvdl
337 1.1.1.2 fvdl if ((unsigned)fd >= fdp->fd_nfiles ||
338 1.1.1.2 fvdl (fp = fdp->fd_ofiles[fd]) == NULL)
339 1.1.1.2 fvdl return (EBADF);
340 1.1.1.2 fvdl pf = (u_char *)&fdp->fd_ofileflags[fd];
341 1.1.1.2 fvdl if (*pf & UF_MAPPED)
342 1.1.1.2 fvdl (void) munmapfd(p, fd);
343 1.1.1.2 fvdl fdp->fd_ofiles[fd] = NULL;
344 1.1.1.2 fvdl while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
345 1.1.1.2 fvdl fdp->fd_lastfile--;
346 1.1.1.2 fvdl if (fd < fdp->fd_freefile)
347 1.1.1.2 fvdl fdp->fd_freefile = fd;
348 1.1.1.2 fvdl *pf = 0;
349 1.1.1.2 fvdl return (closef(fp, p));
350 1.1.1.2 fvdl }
351 1.1.1.2 fvdl
352 1.1.1.2 fvdl #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
353 1.1.1.2 fvdl /*
354 1.1.1.2 fvdl * Return status information about a file descriptor.
355 1.1.1.2 fvdl */
356 1.1.1.2 fvdl struct ofstat_args {
357 1.1.1.2 fvdl int fd;
358 1.1.1.2 fvdl struct ostat *sb;
359 1.1.1.2 fvdl };
360 1.1.1.2 fvdl /* ARGSUSED */
361 1.1.1.2 fvdl ofstat(p, uap, retval)
362 1.1.1.2 fvdl struct proc *p;
363 1.1.1.2 fvdl register struct ofstat_args *uap;
364 1.1.1.2 fvdl int *retval;
365 1.1.1.2 fvdl {
366 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
367 1.1.1.2 fvdl register struct file *fp;
368 1.1.1.2 fvdl struct stat ub;
369 1.1.1.2 fvdl struct ostat oub;
370 1.1.1.2 fvdl int error;
371 1.1.1.2 fvdl
372 1.1.1.2 fvdl if ((unsigned)uap->fd >= fdp->fd_nfiles ||
373 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL)
374 1.1.1.2 fvdl return (EBADF);
375 1.1.1.2 fvdl switch (fp->f_type) {
376 1.1.1.2 fvdl
377 1.1.1.2 fvdl case DTYPE_VNODE:
378 1.1.1.2 fvdl error = vn_stat((struct vnode *)fp->f_data, &ub, p);
379 1.1.1.2 fvdl break;
380 1.1.1.2 fvdl
381 1.1.1.2 fvdl case DTYPE_SOCKET:
382 1.1.1.2 fvdl error = soo_stat((struct socket *)fp->f_data, &ub);
383 1.1.1.2 fvdl break;
384 1.1.1.2 fvdl
385 1.1.1.2 fvdl default:
386 1.1.1.2 fvdl panic("ofstat");
387 1.1.1.2 fvdl /*NOTREACHED*/
388 1.1.1.2 fvdl }
389 1.1.1.2 fvdl cvtstat(&ub, &oub);
390 1.1.1.2 fvdl if (error == 0)
391 1.1.1.2 fvdl error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
392 1.1.1.2 fvdl return (error);
393 1.1.1.2 fvdl }
394 1.1.1.2 fvdl #endif /* COMPAT_43 || COMPAT_SUNOS */
395 1.1.1.2 fvdl
396 1.1.1.2 fvdl /*
397 1.1.1.2 fvdl * Return status information about a file descriptor.
398 1.1.1.2 fvdl */
399 1.1.1.2 fvdl struct fstat_args {
400 1.1.1.2 fvdl int fd;
401 1.1.1.2 fvdl struct stat *sb;
402 1.1.1.2 fvdl };
403 1.1.1.2 fvdl /* ARGSUSED */
404 1.1.1.2 fvdl fstat(p, uap, retval)
405 1.1.1.2 fvdl struct proc *p;
406 1.1.1.2 fvdl register struct fstat_args *uap;
407 1.1.1.2 fvdl int *retval;
408 1.1.1.2 fvdl {
409 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
410 1.1.1.2 fvdl register struct file *fp;
411 1.1.1.2 fvdl struct stat ub;
412 1.1.1.2 fvdl int error;
413 1.1.1.2 fvdl
414 1.1.1.2 fvdl if ((unsigned)uap->fd >= fdp->fd_nfiles ||
415 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL)
416 1.1.1.2 fvdl return (EBADF);
417 1.1.1.2 fvdl switch (fp->f_type) {
418 1.1.1.2 fvdl
419 1.1.1.2 fvdl case DTYPE_VNODE:
420 1.1.1.2 fvdl error = vn_stat((struct vnode *)fp->f_data, &ub, p);
421 1.1.1.2 fvdl break;
422 1.1.1.2 fvdl
423 1.1.1.2 fvdl case DTYPE_SOCKET:
424 1.1.1.2 fvdl error = soo_stat((struct socket *)fp->f_data, &ub);
425 1.1.1.2 fvdl break;
426 1.1.1.2 fvdl
427 1.1.1.2 fvdl default:
428 1.1.1.2 fvdl panic("fstat");
429 1.1.1.2 fvdl /*NOTREACHED*/
430 1.1.1.2 fvdl }
431 1.1.1.2 fvdl if (error == 0)
432 1.1.1.2 fvdl error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
433 1.1.1.2 fvdl return (error);
434 1.1.1.2 fvdl }
435 1.1.1.2 fvdl
436 1.1.1.2 fvdl /*
437 1.1.1.2 fvdl * Return pathconf information about a file descriptor.
438 1.1.1.2 fvdl */
439 1.1.1.2 fvdl struct fpathconf_args {
440 1.1.1.2 fvdl int fd;
441 1.1.1.2 fvdl int name;
442 1.1.1.2 fvdl };
443 1.1.1.2 fvdl /* ARGSUSED */
444 1.1.1.2 fvdl fpathconf(p, uap, retval)
445 1.1.1.2 fvdl struct proc *p;
446 1.1.1.2 fvdl register struct fpathconf_args *uap;
447 1.1.1.2 fvdl int *retval;
448 1.1.1.2 fvdl {
449 1.1.1.2 fvdl struct filedesc *fdp = p->p_fd;
450 1.1.1.2 fvdl struct file *fp;
451 1.1.1.2 fvdl struct vnode *vp;
452 1.1.1.2 fvdl
453 1.1.1.2 fvdl if ((unsigned)uap->fd >= fdp->fd_nfiles ||
454 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL)
455 1.1.1.2 fvdl return (EBADF);
456 1.1.1.2 fvdl switch (fp->f_type) {
457 1.1.1.2 fvdl
458 1.1.1.2 fvdl case DTYPE_SOCKET:
459 1.1.1.2 fvdl if (uap->name != _PC_PIPE_BUF)
460 1.1.1.2 fvdl return (EINVAL);
461 1.1.1.2 fvdl *retval = PIPE_BUF;
462 1.1.1.2 fvdl return (0);
463 1.1.1.2 fvdl
464 1.1.1.2 fvdl case DTYPE_VNODE:
465 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
466 1.1.1.2 fvdl return (VOP_PATHCONF(vp, uap->name, retval));
467 1.1.1.2 fvdl
468 1.1.1.2 fvdl default:
469 1.1.1.2 fvdl panic("fpathconf");
470 1.1.1.2 fvdl }
471 1.1.1.2 fvdl /*NOTREACHED*/
472 1.1.1.2 fvdl }
473 1.1.1.2 fvdl
474 1.1.1.2 fvdl /*
475 1.1.1.2 fvdl * Allocate a file descriptor for the process.
476 1.1.1.2 fvdl */
477 1.1.1.2 fvdl int fdexpand;
478 1.1.1.2 fvdl
479 1.1.1.2 fvdl fdalloc(p, want, result)
480 1.1.1.2 fvdl struct proc *p;
481 1.1.1.2 fvdl int want;
482 1.1.1.2 fvdl int *result;
483 1.1.1.2 fvdl {
484 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
485 1.1.1.2 fvdl register int i;
486 1.1.1.2 fvdl int lim, last, nfiles;
487 1.1.1.2 fvdl struct file **newofile;
488 1.1.1.2 fvdl char *newofileflags;
489 1.1.1.2 fvdl
490 1.1.1.2 fvdl /*
491 1.1.1.2 fvdl * Search for a free descriptor starting at the higher
492 1.1.1.2 fvdl * of want or fd_freefile. If that fails, consider
493 1.1.1.2 fvdl * expanding the ofile array.
494 1.1.1.2 fvdl */
495 1.1.1.2 fvdl lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
496 1.1.1.2 fvdl for (;;) {
497 1.1.1.2 fvdl last = min(fdp->fd_nfiles, lim);
498 1.1.1.2 fvdl if ((i = want) < fdp->fd_freefile)
499 1.1.1.2 fvdl i = fdp->fd_freefile;
500 1.1.1.2 fvdl for (; i < last; i++) {
501 1.1.1.2 fvdl if (fdp->fd_ofiles[i] == NULL) {
502 1.1.1.2 fvdl fdp->fd_ofileflags[i] = 0;
503 1.1.1.2 fvdl if (i > fdp->fd_lastfile)
504 1.1.1.2 fvdl fdp->fd_lastfile = i;
505 1.1.1.2 fvdl if (want <= fdp->fd_freefile)
506 1.1.1.2 fvdl fdp->fd_freefile = i;
507 1.1.1.2 fvdl *result = i;
508 1.1.1.2 fvdl return (0);
509 1.1.1.2 fvdl }
510 1.1.1.2 fvdl }
511 1.1.1.2 fvdl
512 1.1.1.2 fvdl /*
513 1.1.1.2 fvdl * No space in current array. Expand?
514 1.1.1.2 fvdl */
515 1.1.1.2 fvdl if (fdp->fd_nfiles >= lim)
516 1.1.1.2 fvdl return (EMFILE);
517 1.1.1.2 fvdl if (fdp->fd_nfiles < NDEXTENT)
518 1.1.1.2 fvdl nfiles = NDEXTENT;
519 1.1.1.2 fvdl else
520 1.1.1.2 fvdl nfiles = 2 * fdp->fd_nfiles;
521 1.1.1.2 fvdl MALLOC(newofile, struct file **, nfiles * OFILESIZE,
522 1.1.1.2 fvdl M_FILEDESC, M_WAITOK);
523 1.1.1.2 fvdl newofileflags = (char *) &newofile[nfiles];
524 1.1.1.2 fvdl /*
525 1.1.1.2 fvdl * Copy the existing ofile and ofileflags arrays
526 1.1.1.2 fvdl * and zero the new portion of each array.
527 1.1.1.2 fvdl */
528 1.1.1.2 fvdl bcopy(fdp->fd_ofiles, newofile,
529 1.1.1.2 fvdl (i = sizeof(struct file *) * fdp->fd_nfiles));
530 1.1.1.2 fvdl bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
531 1.1.1.2 fvdl bcopy(fdp->fd_ofileflags, newofileflags,
532 1.1.1.2 fvdl (i = sizeof(char) * fdp->fd_nfiles));
533 1.1.1.2 fvdl bzero(newofileflags + i, nfiles * sizeof(char) - i);
534 1.1.1.2 fvdl if (fdp->fd_nfiles > NDFILE)
535 1.1.1.2 fvdl FREE(fdp->fd_ofiles, M_FILEDESC);
536 1.1.1.2 fvdl fdp->fd_ofiles = newofile;
537 1.1.1.2 fvdl fdp->fd_ofileflags = newofileflags;
538 1.1.1.2 fvdl fdp->fd_nfiles = nfiles;
539 1.1.1.2 fvdl fdexpand++;
540 1.1.1.2 fvdl }
541 1.1.1.2 fvdl }
542 1.1.1.2 fvdl
543 1.1.1.2 fvdl /*
544 1.1.1.2 fvdl * Check to see whether n user file descriptors
545 1.1.1.2 fvdl * are available to the process p.
546 1.1.1.2 fvdl */
547 1.1.1.2 fvdl fdavail(p, n)
548 1.1.1.2 fvdl struct proc *p;
549 1.1.1.2 fvdl register int n;
550 1.1.1.2 fvdl {
551 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
552 1.1.1.2 fvdl register struct file **fpp;
553 1.1.1.2 fvdl register int i, lim;
554 1.1.1.2 fvdl
555 1.1.1.2 fvdl lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
556 1.1.1.2 fvdl if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
557 1.1.1.2 fvdl return (1);
558 1.1.1.2 fvdl fpp = &fdp->fd_ofiles[fdp->fd_freefile];
559 1.1.1.2 fvdl for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
560 1.1.1.2 fvdl if (*fpp == NULL && --n <= 0)
561 1.1.1.2 fvdl return (1);
562 1.1.1.2 fvdl return (0);
563 1.1.1.2 fvdl }
564 1.1.1.2 fvdl
565 1.1.1.2 fvdl /*
566 1.1.1.2 fvdl * Create a new open file structure and allocate
567 1.1.1.2 fvdl * a file decriptor for the process that refers to it.
568 1.1.1.2 fvdl */
569 1.1.1.2 fvdl falloc(p, resultfp, resultfd)
570 1.1.1.2 fvdl register struct proc *p;
571 1.1.1.2 fvdl struct file **resultfp;
572 1.1.1.2 fvdl int *resultfd;
573 1.1.1.2 fvdl {
574 1.1.1.2 fvdl register struct file *fp, *fq, **fpp;
575 1.1.1.2 fvdl int error, i;
576 1.1.1.2 fvdl
577 1.1.1.2 fvdl if (error = fdalloc(p, 0, &i))
578 1.1.1.2 fvdl return (error);
579 1.1.1.2 fvdl if (nfiles >= maxfiles) {
580 1.1.1.2 fvdl tablefull("file");
581 1.1.1.2 fvdl return (ENFILE);
582 1.1.1.2 fvdl }
583 1.1.1.2 fvdl /*
584 1.1.1.2 fvdl * Allocate a new file descriptor.
585 1.1.1.2 fvdl * If the process has file descriptor zero open, add to the list
586 1.1.1.2 fvdl * of open files at that point, otherwise put it at the front of
587 1.1.1.2 fvdl * the list of open files.
588 1.1.1.2 fvdl */
589 1.1.1.2 fvdl nfiles++;
590 1.1.1.2 fvdl MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
591 1.1.1.2 fvdl bzero(fp, sizeof(struct file));
592 1.1.1.2 fvdl if (fq = p->p_fd->fd_ofiles[0])
593 1.1.1.2 fvdl fpp = &fq->f_filef;
594 1.1.1.2 fvdl else
595 1.1.1.2 fvdl fpp = &filehead;
596 1.1.1.2 fvdl p->p_fd->fd_ofiles[i] = fp;
597 1.1.1.2 fvdl if (fq = *fpp)
598 1.1.1.2 fvdl fq->f_fileb = &fp->f_filef;
599 1.1.1.2 fvdl fp->f_filef = fq;
600 1.1.1.2 fvdl fp->f_fileb = fpp;
601 1.1.1.2 fvdl *fpp = fp;
602 1.1.1.2 fvdl fp->f_count = 1;
603 1.1.1.2 fvdl fp->f_cred = p->p_ucred;
604 1.1.1.2 fvdl crhold(fp->f_cred);
605 1.1.1.2 fvdl if (resultfp)
606 1.1.1.2 fvdl *resultfp = fp;
607 1.1.1.2 fvdl if (resultfd)
608 1.1.1.2 fvdl *resultfd = i;
609 1.1.1.2 fvdl return (0);
610 1.1.1.2 fvdl }
611 1.1.1.2 fvdl
612 1.1.1.2 fvdl /*
613 1.1.1.2 fvdl * Free a file descriptor.
614 1.1.1.2 fvdl */
615 1.1.1.2 fvdl ffree(fp)
616 1.1.1.2 fvdl register struct file *fp;
617 1.1.1.2 fvdl {
618 1.1.1.2 fvdl register struct file *fq;
619 1.1.1.2 fvdl
620 1.1.1.2 fvdl if (fq = fp->f_filef)
621 1.1.1.2 fvdl fq->f_fileb = fp->f_fileb;
622 1.1.1.2 fvdl *fp->f_fileb = fq;
623 1.1.1.2 fvdl crfree(fp->f_cred);
624 1.1.1.2 fvdl #ifdef DIAGNOSTIC
625 1.1.1.2 fvdl fp->f_filef = NULL;
626 1.1.1.2 fvdl fp->f_fileb = NULL;
627 1.1.1.2 fvdl fp->f_count = 0;
628 1.1.1.2 fvdl #endif
629 1.1.1.2 fvdl nfiles--;
630 1.1.1.2 fvdl FREE(fp, M_FILE);
631 1.1.1.2 fvdl }
632 1.1.1.2 fvdl
633 1.1.1.2 fvdl /*
634 1.1.1.2 fvdl * Copy a filedesc structure.
635 1.1.1.2 fvdl */
636 1.1.1.2 fvdl struct filedesc *
637 1.1.1.2 fvdl fdcopy(p)
638 1.1.1.2 fvdl struct proc *p;
639 1.1.1.2 fvdl {
640 1.1.1.2 fvdl register struct filedesc *newfdp, *fdp = p->p_fd;
641 1.1.1.2 fvdl register struct file **fpp;
642 1.1.1.2 fvdl register int i;
643 1.1.1.2 fvdl
644 1.1.1.2 fvdl MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
645 1.1.1.2 fvdl M_FILEDESC, M_WAITOK);
646 1.1.1.2 fvdl bcopy(fdp, newfdp, sizeof(struct filedesc));
647 1.1.1.2 fvdl VREF(newfdp->fd_cdir);
648 1.1.1.2 fvdl if (newfdp->fd_rdir)
649 1.1.1.2 fvdl VREF(newfdp->fd_rdir);
650 1.1.1.2 fvdl newfdp->fd_refcnt = 1;
651 1.1.1.2 fvdl
652 1.1.1.2 fvdl /*
653 1.1.1.2 fvdl * If the number of open files fits in the internal arrays
654 1.1.1.2 fvdl * of the open file structure, use them, otherwise allocate
655 1.1.1.2 fvdl * additional memory for the number of descriptors currently
656 1.1.1.2 fvdl * in use.
657 1.1.1.2 fvdl */
658 1.1.1.2 fvdl if (newfdp->fd_lastfile < NDFILE) {
659 1.1.1.2 fvdl newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
660 1.1.1.2 fvdl newfdp->fd_ofileflags =
661 1.1.1.2 fvdl ((struct filedesc0 *) newfdp)->fd_dfileflags;
662 1.1.1.2 fvdl i = NDFILE;
663 1.1.1.2 fvdl } else {
664 1.1.1.2 fvdl /*
665 1.1.1.2 fvdl * Compute the smallest multiple of NDEXTENT needed
666 1.1.1.2 fvdl * for the file descriptors currently in use,
667 1.1.1.2 fvdl * allowing the table to shrink.
668 1.1.1.2 fvdl */
669 1.1.1.2 fvdl i = newfdp->fd_nfiles;
670 1.1.1.2 fvdl while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
671 1.1.1.2 fvdl i /= 2;
672 1.1.1.2 fvdl MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
673 1.1.1.2 fvdl M_FILEDESC, M_WAITOK);
674 1.1.1.2 fvdl newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
675 1.1.1.2 fvdl }
676 1.1.1.2 fvdl newfdp->fd_nfiles = i;
677 1.1.1.2 fvdl bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
678 1.1.1.2 fvdl bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
679 1.1.1.2 fvdl fpp = newfdp->fd_ofiles;
680 1.1.1.2 fvdl for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
681 1.1.1.2 fvdl if (*fpp != NULL)
682 1.1.1.2 fvdl (*fpp)->f_count++;
683 1.1.1.2 fvdl return (newfdp);
684 1.1.1.2 fvdl }
685 1.1.1.2 fvdl
686 1.1.1.2 fvdl /*
687 1.1.1.2 fvdl * Release a filedesc structure.
688 1.1.1.2 fvdl */
689 1.1.1.2 fvdl void
690 1.1.1.2 fvdl fdfree(p)
691 1.1.1.2 fvdl struct proc *p;
692 1.1.1.2 fvdl {
693 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
694 1.1.1.2 fvdl struct file **fpp;
695 1.1.1.2 fvdl register int i;
696 1.1.1.2 fvdl
697 1.1.1.2 fvdl if (--fdp->fd_refcnt > 0)
698 1.1.1.2 fvdl return;
699 1.1.1.2 fvdl fpp = fdp->fd_ofiles;
700 1.1.1.2 fvdl for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
701 1.1.1.2 fvdl if (*fpp)
702 1.1.1.2 fvdl (void) closef(*fpp, p);
703 1.1.1.2 fvdl if (fdp->fd_nfiles > NDFILE)
704 1.1.1.2 fvdl FREE(fdp->fd_ofiles, M_FILEDESC);
705 1.1.1.2 fvdl vrele(fdp->fd_cdir);
706 1.1.1.2 fvdl if (fdp->fd_rdir)
707 1.1.1.2 fvdl vrele(fdp->fd_rdir);
708 1.1.1.2 fvdl FREE(fdp, M_FILEDESC);
709 1.1.1.2 fvdl }
710 1.1.1.2 fvdl
711 1.1.1.2 fvdl /*
712 1.1.1.2 fvdl * Internal form of close.
713 1.1.1.2 fvdl * Decrement reference count on file structure.
714 1.1.1.2 fvdl * Note: p may be NULL when closing a file
715 1.1.1.2 fvdl * that was being passed in a message.
716 1.1.1.2 fvdl */
717 1.1.1.2 fvdl closef(fp, p)
718 1.1.1.2 fvdl register struct file *fp;
719 1.1.1.2 fvdl register struct proc *p;
720 1.1.1.2 fvdl {
721 1.1.1.2 fvdl struct vnode *vp;
722 1.1.1.2 fvdl struct flock lf;
723 1.1.1.2 fvdl int error;
724 1.1.1.2 fvdl
725 1.1.1.2 fvdl if (fp == NULL)
726 1.1.1.2 fvdl return (0);
727 1.1.1.2 fvdl /*
728 1.1.1.2 fvdl * POSIX record locking dictates that any close releases ALL
729 1.1.1.2 fvdl * locks owned by this process. This is handled by setting
730 1.1.1.2 fvdl * a flag in the unlock to free ONLY locks obeying POSIX
731 1.1.1.2 fvdl * semantics, and not to free BSD-style file locks.
732 1.1.1.2 fvdl * If the descriptor was in a message, POSIX-style locks
733 1.1.1.2 fvdl * aren't passed with the descriptor.
734 1.1.1.2 fvdl */
735 1.1.1.2 fvdl if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
736 1.1.1.2 fvdl lf.l_whence = SEEK_SET;
737 1.1.1.2 fvdl lf.l_start = 0;
738 1.1.1.2 fvdl lf.l_len = 0;
739 1.1.1.2 fvdl lf.l_type = F_UNLCK;
740 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
741 1.1.1.2 fvdl (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
742 1.1.1.2 fvdl }
743 1.1.1.2 fvdl if (--fp->f_count > 0)
744 1.1.1.2 fvdl return (0);
745 1.1.1.2 fvdl if (fp->f_count < 0)
746 1.1.1.2 fvdl panic("closef: count < 0");
747 1.1.1.2 fvdl if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
748 1.1.1.2 fvdl lf.l_whence = SEEK_SET;
749 1.1.1.2 fvdl lf.l_start = 0;
750 1.1.1.2 fvdl lf.l_len = 0;
751 1.1.1.2 fvdl lf.l_type = F_UNLCK;
752 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
753 1.1.1.2 fvdl (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
754 1.1.1.2 fvdl }
755 1.1.1.2 fvdl if (fp->f_ops)
756 1.1.1.2 fvdl error = (*fp->f_ops->fo_close)(fp, p);
757 1.1.1.2 fvdl else
758 1.1.1.2 fvdl error = 0;
759 1.1.1.2 fvdl ffree(fp);
760 1.1.1.2 fvdl return (error);
761 1.1.1.2 fvdl }
762 1.1.1.2 fvdl
763 1.1.1.2 fvdl /*
764 1.1.1.2 fvdl * Apply an advisory lock on a file descriptor.
765 1.1.1.2 fvdl *
766 1.1.1.2 fvdl * Just attempt to get a record lock of the requested type on
767 1.1.1.2 fvdl * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
768 1.1.1.2 fvdl */
769 1.1.1.2 fvdl struct flock_args {
770 1.1.1.2 fvdl int fd;
771 1.1.1.2 fvdl int how;
772 1.1.1.2 fvdl };
773 1.1.1.2 fvdl /* ARGSUSED */
774 1.1.1.2 fvdl flock(p, uap, retval)
775 1.1.1.2 fvdl struct proc *p;
776 1.1.1.2 fvdl register struct flock_args *uap;
777 1.1.1.2 fvdl int *retval;
778 1.1.1.2 fvdl {
779 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
780 1.1.1.2 fvdl register struct file *fp;
781 1.1.1.2 fvdl struct vnode *vp;
782 1.1.1.2 fvdl struct flock lf;
783 1.1.1.2 fvdl
784 1.1.1.2 fvdl if ((unsigned)uap->fd >= fdp->fd_nfiles ||
785 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL)
786 1.1.1.2 fvdl return (EBADF);
787 1.1.1.2 fvdl if (fp->f_type != DTYPE_VNODE)
788 1.1.1.2 fvdl return (EOPNOTSUPP);
789 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
790 1.1.1.2 fvdl lf.l_whence = SEEK_SET;
791 1.1.1.2 fvdl lf.l_start = 0;
792 1.1.1.2 fvdl lf.l_len = 0;
793 1.1.1.2 fvdl if (uap->how & LOCK_UN) {
794 1.1.1.2 fvdl lf.l_type = F_UNLCK;
795 1.1.1.2 fvdl fp->f_flag &= ~FHASLOCK;
796 1.1.1.2 fvdl return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
797 1.1.1.2 fvdl }
798 1.1.1.2 fvdl if (uap->how & LOCK_EX)
799 1.1.1.2 fvdl lf.l_type = F_WRLCK;
800 1.1.1.2 fvdl else if (uap->how & LOCK_SH)
801 1.1.1.2 fvdl lf.l_type = F_RDLCK;
802 1.1.1.2 fvdl else
803 1.1.1.2 fvdl return (EBADF);
804 1.1.1.2 fvdl fp->f_flag |= FHASLOCK;
805 1.1.1.2 fvdl if (uap->how & LOCK_NB)
806 1.1.1.2 fvdl return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
807 1.1.1.2 fvdl return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
808 1.1.1.2 fvdl }
809 1.1.1.2 fvdl
810 1.1.1.2 fvdl /*
811 1.1.1.2 fvdl * File Descriptor pseudo-device driver (/dev/fd/).
812 1.1.1.2 fvdl *
813 1.1.1.2 fvdl * Opening minor device N dup()s the file (if any) connected to file
814 1.1.1.2 fvdl * descriptor N belonging to the calling process. Note that this driver
815 1.1.1.2 fvdl * consists of only the ``open()'' routine, because all subsequent
816 1.1.1.2 fvdl * references to this file will be direct to the other driver.
817 1.1.1.2 fvdl */
818 1.1.1.2 fvdl /* ARGSUSED */
819 1.1.1.2 fvdl fdopen(dev, mode, type, p)
820 1.1.1.2 fvdl dev_t dev;
821 1.1.1.2 fvdl int mode, type;
822 1.1.1.2 fvdl struct proc *p;
823 1.1.1.2 fvdl {
824 1.1.1.2 fvdl
825 1.1.1.2 fvdl /*
826 1.1.1.2 fvdl * XXX Kludge: set curproc->p_dupfd to contain the value of the
827 1.1.1.2 fvdl * the file descriptor being sought for duplication. The error
828 1.1.1.2 fvdl * return ensures that the vnode for this device will be released
829 1.1.1.2 fvdl * by vn_open. Open will detect this special error and take the
830 1.1.1.2 fvdl * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
831 1.1.1.2 fvdl * will simply report the error.
832 1.1.1.2 fvdl */
833 1.1.1.2 fvdl p->p_dupfd = minor(dev);
834 1.1.1.2 fvdl return (ENODEV);
835 1.1.1.2 fvdl }
836 1.1.1.2 fvdl
837 1.1.1.2 fvdl /*
838 1.1.1.2 fvdl * Duplicate the specified descriptor to a free descriptor.
839 1.1.1.2 fvdl */
840 1.1.1.2 fvdl dupfdopen(fdp, indx, dfd, mode, error)
841 1.1.1.2 fvdl register struct filedesc *fdp;
842 1.1.1.2 fvdl register int indx, dfd;
843 1.1.1.2 fvdl int mode;
844 1.1.1.2 fvdl int error;
845 1.1.1.2 fvdl {
846 1.1.1.2 fvdl register struct file *wfp;
847 1.1.1.2 fvdl struct file *fp;
848 1.1.1.2 fvdl
849 1.1.1.2 fvdl /*
850 1.1.1.2 fvdl * If the to-be-dup'd fd number is greater than the allowed number
851 1.1.1.2 fvdl * of file descriptors, or the fd to be dup'd has already been
852 1.1.1.2 fvdl * closed, reject. Note, check for new == old is necessary as
853 1.1.1.2 fvdl * falloc could allocate an already closed to-be-dup'd descriptor
854 1.1.1.2 fvdl * as the new descriptor.
855 1.1.1.2 fvdl */
856 1.1.1.2 fvdl fp = fdp->fd_ofiles[indx];
857 1.1.1.2 fvdl if ((u_int)dfd >= fdp->fd_nfiles ||
858 1.1.1.2 fvdl (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
859 1.1.1.2 fvdl return (EBADF);
860 1.1.1.2 fvdl
861 1.1.1.2 fvdl /*
862 1.1.1.2 fvdl * There are two cases of interest here.
863 1.1.1.2 fvdl *
864 1.1.1.2 fvdl * For ENODEV simply dup (dfd) to file descriptor
865 1.1.1.2 fvdl * (indx) and return.
866 1.1.1.2 fvdl *
867 1.1.1.2 fvdl * For ENXIO steal away the file structure from (dfd) and
868 1.1.1.2 fvdl * store it in (indx). (dfd) is effectively closed by
869 1.1.1.2 fvdl * this operation.
870 1.1.1.2 fvdl *
871 1.1.1.2 fvdl * Any other error code is just returned.
872 1.1.1.2 fvdl */
873 1.1.1.2 fvdl switch (error) {
874 1.1.1.2 fvdl case ENODEV:
875 1.1.1.2 fvdl /*
876 1.1.1.2 fvdl * Check that the mode the file is being opened for is a
877 1.1.1.2 fvdl * subset of the mode of the existing descriptor.
878 1.1.1.2 fvdl */
879 1.1.1.2 fvdl if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
880 1.1.1.2 fvdl return (EACCES);
881 1.1.1.2 fvdl fdp->fd_ofiles[indx] = wfp;
882 1.1.1.2 fvdl fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
883 1.1.1.2 fvdl wfp->f_count++;
884 1.1.1.2 fvdl if (indx > fdp->fd_lastfile)
885 1.1.1.2 fvdl fdp->fd_lastfile = indx;
886 1.1.1.2 fvdl return (0);
887 1.1.1.2 fvdl
888 1.1.1.2 fvdl case ENXIO:
889 1.1.1.2 fvdl /*
890 1.1.1.2 fvdl * Steal away the file pointer from dfd, and stuff it into indx.
891 1.1.1.2 fvdl */
892 1.1.1.2 fvdl fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
893 1.1.1.2 fvdl fdp->fd_ofiles[dfd] = NULL;
894 1.1.1.2 fvdl fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
895 1.1.1.2 fvdl fdp->fd_ofileflags[dfd] = 0;
896 1.1.1.2 fvdl /*
897 1.1.1.2 fvdl * Complete the clean up of the filedesc structure by
898 1.1.1.2 fvdl * recomputing the various hints.
899 1.1.1.2 fvdl */
900 1.1.1.2 fvdl if (indx > fdp->fd_lastfile)
901 1.1.1.2 fvdl fdp->fd_lastfile = indx;
902 1.1.1.2 fvdl else
903 1.1.1.2 fvdl while (fdp->fd_lastfile > 0 &&
904 1.1.1.2 fvdl fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
905 1.1.1.2 fvdl fdp->fd_lastfile--;
906 1.1.1.2 fvdl if (dfd < fdp->fd_freefile)
907 1.1.1.2 fvdl fdp->fd_freefile = dfd;
908 1.1.1.2 fvdl return (0);
909 1.1.1.2 fvdl
910 1.1.1.2 fvdl default:
911 1.1.1.2 fvdl return (error);
912 1.1.1.2 fvdl }
913 1.1.1.2 fvdl /* NOTREACHED */
914 1.1.1.2 fvdl }
915