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