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