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