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