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