Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
7ddc4f96 | 2 | * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California. |
c4ec2128 | 3 | * All rights reserved. |
da7c5cc6 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
c4ec2128 | 6 | * |
c1884703 | 7 | * @(#)kern_descrip.c 7.28 (Berkeley) %G% |
da7c5cc6 | 8 | */ |
40056b21 | 9 | |
94368568 JB |
10 | #include "param.h" |
11 | #include "systm.h" | |
5e00df3b | 12 | #include "filedesc.h" |
94368568 | 13 | #include "kernel.h" |
c4ec2128 | 14 | #include "vnode.h" |
94368568 | 15 | #include "proc.h" |
94368568 JB |
16 | #include "file.h" |
17 | #include "socket.h" | |
18 | #include "socketvar.h" | |
94368568 | 19 | #include "stat.h" |
94368568 | 20 | #include "ioctl.h" |
a4128336 | 21 | #include "fcntl.h" |
5e00df3b KM |
22 | #include "malloc.h" |
23 | #include "syslog.h" | |
7ddc4f96 | 24 | #include "resourcevar.h" |
4147b3f6 BJ |
25 | |
26 | /* | |
27 | * Descriptor management. | |
28 | */ | |
c753a490 KM |
29 | struct file *filehead; /* head of list of open files */ |
30 | int nfiles; /* actual number of open files */ | |
4147b3f6 | 31 | |
40056b21 | 32 | /* |
4147b3f6 | 33 | * System calls on descriptors. |
40056b21 | 34 | */ |
a53a698b KM |
35 | /* ARGSUSED */ |
36 | getdtablesize(p, uap, retval) | |
37 | struct proc *p; | |
38 | struct args *uap; | |
39 | int *retval; | |
4147b3f6 BJ |
40 | { |
41 | ||
8429d022 | 42 | *retval = p->p_rlimit[RLIMIT_OFILE].rlim_cur; |
d9c2f47f | 43 | return (0); |
4147b3f6 BJ |
44 | } |
45 | ||
a53a698b KM |
46 | /* |
47 | * Duplicate a file descriptor. | |
48 | */ | |
49 | /* ARGSUSED */ | |
50 | dup(p, uap, retval) | |
51 | struct proc *p; | |
52 | struct args { | |
4147b3f6 | 53 | int i; |
a53a698b KM |
54 | } *uap; |
55 | int *retval; | |
56 | { | |
5e00df3b | 57 | register struct filedesc *fdp = p->p_fd; |
a81e9a81 | 58 | struct file *fp; |
a53a698b | 59 | int fd, error; |
4147b3f6 | 60 | |
a53a698b KM |
61 | /* |
62 | * XXX Compatibility | |
63 | */ | |
d9c2f47f | 64 | if (uap->i &~ 077) { uap->i &= 077; return (dup2(p, uap, retval)); } |
4147b3f6 | 65 | |
8429d022 | 66 | if ((unsigned)uap->i >= fdp->fd_nfiles || |
7ddc4f96 | 67 | (fp = fdp->fd_ofiles[uap->i]) == NULL) |
d9c2f47f | 68 | return (EBADF); |
8429d022 | 69 | if (error = fdalloc(p, 0, &fd)) |
d9c2f47f | 70 | return (error); |
7ddc4f96 MK |
71 | fdp->fd_ofiles[fd] = fp; |
72 | fdp->fd_ofileflags[fd] = fdp->fd_ofileflags[uap->i] &~ UF_EXCLOSE; | |
a53a698b | 73 | fp->f_count++; |
5e00df3b KM |
74 | if (fd > fdp->fd_lastfile) |
75 | fdp->fd_lastfile = fd; | |
a53a698b | 76 | *retval = fd; |
d9c2f47f | 77 | return (0); |
4147b3f6 BJ |
78 | } |
79 | ||
a53a698b KM |
80 | /* |
81 | * Duplicate a file descriptor to a particular value. | |
82 | */ | |
83 | /* ARGSUSED */ | |
84 | dup2(p, uap, retval) | |
85 | struct proc *p; | |
7ddc4f96 MK |
86 | struct args { |
87 | u_int from; | |
88 | u_int to; | |
a53a698b KM |
89 | } *uap; |
90 | int *retval; | |
4147b3f6 | 91 | { |
5e00df3b | 92 | register struct filedesc *fdp = p->p_fd; |
4147b3f6 | 93 | register struct file *fp; |
7ddc4f96 | 94 | register u_int old = uap->from, new = uap->to; |
5e00df3b | 95 | int i, error; |
40056b21 | 96 | |
7ddc4f96 MK |
97 | if (old >= fdp->fd_nfiles || |
98 | (fp = fdp->fd_ofiles[old]) == NULL || | |
99 | new >= p->p_rlimit[RLIMIT_OFILE].rlim_cur) | |
d9c2f47f | 100 | return (EBADF); |
7ddc4f96 MK |
101 | *retval = new; |
102 | if (old == new) | |
d9c2f47f | 103 | return (0); |
7ddc4f96 MK |
104 | if (new >= fdp->fd_nfiles) { |
105 | if (error = fdalloc(p, new, &i)) | |
5e00df3b | 106 | return (error); |
7ddc4f96 | 107 | if (new != i) |
8429d022 | 108 | panic("dup2: fdalloc"); |
7ddc4f96 MK |
109 | } else if (fdp->fd_ofiles[new]) { |
110 | if (fdp->fd_ofileflags[new] & UF_MAPPED) | |
111 | (void) munmapfd(p, new); | |
112 | /* | |
113 | * dup2() must succeed even if the close has an error. | |
114 | */ | |
115 | (void) closef(fdp->fd_ofiles[new], p); | |
4147b3f6 | 116 | } |
7ddc4f96 MK |
117 | fdp->fd_ofiles[new] = fp; |
118 | fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; | |
a53a698b | 119 | fp->f_count++; |
7ddc4f96 MK |
120 | if (new > fdp->fd_lastfile) |
121 | fdp->fd_lastfile = new; | |
122 | return (0); | |
a81e9a81 SL |
123 | } |
124 | ||
88a7a62a SL |
125 | /* |
126 | * The file control system call. | |
127 | */ | |
a53a698b KM |
128 | /* ARGSUSED */ |
129 | fcntl(p, uap, retval) | |
130 | struct proc *p; | |
131 | register struct args { | |
8429d022 | 132 | int fd; |
88a7a62a SL |
133 | int cmd; |
134 | int arg; | |
a53a698b KM |
135 | } *uap; |
136 | int *retval; | |
137 | { | |
5e00df3b | 138 | register struct filedesc *fdp = p->p_fd; |
a53a698b | 139 | register struct file *fp; |
88a7a62a | 140 | register char *pop; |
a4128336 | 141 | struct vnode *vp; |
208eb71b | 142 | int i, tmp, error, flg = F_POSIX; |
a4128336 | 143 | struct flock fl; |
4147b3f6 | 144 | |
8429d022 | 145 | if ((unsigned)uap->fd >= fdp->fd_nfiles || |
7ddc4f96 | 146 | (fp = fdp->fd_ofiles[uap->fd]) == NULL) |
d9c2f47f | 147 | return (EBADF); |
7ddc4f96 | 148 | pop = &fdp->fd_ofileflags[uap->fd]; |
88a7a62a | 149 | switch(uap->cmd) { |
85f01bd4 | 150 | case F_DUPFD: |
8429d022 | 151 | if ((unsigned)uap->arg >= p->p_rlimit[RLIMIT_OFILE].rlim_cur) |
d9c2f47f | 152 | return (EINVAL); |
8429d022 | 153 | if (error = fdalloc(p, uap->arg, &i)) |
d9c2f47f | 154 | return (error); |
7ddc4f96 MK |
155 | fdp->fd_ofiles[i] = fp; |
156 | fdp->fd_ofileflags[i] = *pop &~ UF_EXCLOSE; | |
a53a698b | 157 | fp->f_count++; |
5e00df3b KM |
158 | if (i > fdp->fd_lastfile) |
159 | fdp->fd_lastfile = i; | |
a53a698b | 160 | *retval = i; |
d9c2f47f | 161 | return (0); |
12438177 | 162 | |
85f01bd4 | 163 | case F_GETFD: |
a53a698b | 164 | *retval = *pop & 1; |
d9c2f47f | 165 | return (0); |
40056b21 | 166 | |
85f01bd4 | 167 | case F_SETFD: |
88a7a62a | 168 | *pop = (*pop &~ 1) | (uap->arg & 1); |
d9c2f47f | 169 | return (0); |
12438177 | 170 | |
85f01bd4 | 171 | case F_GETFL: |
f474c4c6 | 172 | *retval = OFLAGS(fp->f_flag); |
d9c2f47f | 173 | return (0); |
12438177 | 174 | |
85f01bd4 | 175 | case F_SETFL: |
f474c4c6 MK |
176 | fp->f_flag &= ~FCNTLFLAGS; |
177 | fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS; | |
ff0803f0 | 178 | tmp = fp->f_flag & FNONBLOCK; |
208eb71b KM |
179 | error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p); |
180 | if (error) | |
d9c2f47f | 181 | return (error); |
ff0803f0 | 182 | tmp = fp->f_flag & FASYNC; |
208eb71b KM |
183 | error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p); |
184 | if (!error) | |
185 | return (0); | |
ff0803f0 | 186 | fp->f_flag &= ~FNONBLOCK; |
208eb71b KM |
187 | tmp = 0; |
188 | (void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p); | |
d9c2f47f | 189 | return (error); |
88a7a62a | 190 | |
85f01bd4 | 191 | case F_GETOWN: |
208eb71b KM |
192 | if (fp->f_type == DTYPE_SOCKET) { |
193 | *retval = ((struct socket *)fp->f_data)->so_pgid; | |
194 | return (0); | |
195 | } | |
196 | error = (*fp->f_ops->fo_ioctl) | |
197 | (fp, (int)TIOCGPGRP, (caddr_t)retval, p); | |
198 | *retval = -*retval; | |
199 | return (error); | |
88a7a62a | 200 | |
85f01bd4 | 201 | case F_SETOWN: |
208eb71b KM |
202 | if (fp->f_type == DTYPE_SOCKET) { |
203 | ((struct socket *)fp->f_data)->so_pgid = uap->arg; | |
204 | return (0); | |
205 | } | |
206 | if (uap->arg <= 0) { | |
207 | uap->arg = -uap->arg; | |
208 | } else { | |
209 | struct proc *p1 = pfind(uap->arg); | |
210 | if (p1 == 0) | |
211 | return (ESRCH); | |
212 | uap->arg = p1->p_pgrp->pg_id; | |
213 | } | |
214 | return ((*fp->f_ops->fo_ioctl) | |
215 | (fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p)); | |
88a7a62a | 216 | |
a4128336 | 217 | case F_SETLKW: |
208eb71b | 218 | flg |= F_WAIT; |
a4128336 KM |
219 | /* Fall into F_SETLK */ |
220 | ||
221 | case F_SETLK: | |
222 | if (fp->f_type != DTYPE_VNODE) | |
223 | return (EBADF); | |
224 | vp = (struct vnode *)fp->f_data; | |
225 | /* Copy in the lock structure */ | |
226 | error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl)); | |
227 | if (error) | |
228 | return (error); | |
229 | if (fl.l_whence == SEEK_CUR) | |
230 | fl.l_start += fp->f_offset; | |
231 | switch (fl.l_type) { | |
232 | ||
233 | case F_RDLCK: | |
234 | if ((fp->f_flag & FREAD) == 0) | |
235 | return (EBADF); | |
9bd5ee04 | 236 | p->p_flag |= SADVLCK; |
208eb71b | 237 | return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg)); |
a4128336 KM |
238 | |
239 | case F_WRLCK: | |
240 | if ((fp->f_flag & FWRITE) == 0) | |
241 | return (EBADF); | |
9bd5ee04 | 242 | p->p_flag |= SADVLCK; |
208eb71b | 243 | return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg)); |
a4128336 KM |
244 | |
245 | case F_UNLCK: | |
208eb71b KM |
246 | return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl, |
247 | F_POSIX)); | |
a4128336 KM |
248 | |
249 | default: | |
250 | return (EINVAL); | |
251 | } | |
252 | ||
253 | case F_GETLK: | |
254 | if (fp->f_type != DTYPE_VNODE) | |
255 | return (EBADF); | |
256 | vp = (struct vnode *)fp->f_data; | |
257 | /* Copy in the lock structure */ | |
258 | error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl)); | |
259 | if (error) | |
260 | return (error); | |
2975e814 KM |
261 | if (fl.l_whence == SEEK_CUR) |
262 | fl.l_start += fp->f_offset; | |
208eb71b | 263 | if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX)) |
a4128336 KM |
264 | return (error); |
265 | return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl))); | |
266 | ||
88a7a62a | 267 | default: |
d9c2f47f | 268 | return (EINVAL); |
40056b21 | 269 | } |
a53a698b | 270 | /* NOTREACHED */ |
40056b21 BJ |
271 | } |
272 | ||
a53a698b KM |
273 | /* |
274 | * Close a file descriptor. | |
275 | */ | |
276 | /* ARGSUSED */ | |
277 | close(p, uap, retval) | |
278 | struct proc *p; | |
279 | struct args { | |
8429d022 | 280 | int fd; |
a53a698b KM |
281 | } *uap; |
282 | int *retval; | |
283 | { | |
5e00df3b | 284 | register struct filedesc *fdp = p->p_fd; |
88a7a62a | 285 | register struct file *fp; |
8429d022 | 286 | register int fd = uap->fd; |
92438dfc | 287 | register u_char *pf; |
3ebb7a40 | 288 | |
8429d022 | 289 | if ((unsigned)fd >= fdp->fd_nfiles || |
7ddc4f96 | 290 | (fp = fdp->fd_ofiles[fd]) == NULL) |
d9c2f47f | 291 | return (EBADF); |
7ddc4f96 | 292 | pf = (u_char *)&fdp->fd_ofileflags[fd]; |
92438dfc | 293 | if (*pf & UF_MAPPED) |
8429d022 | 294 | (void) munmapfd(p, fd); |
7ddc4f96 MK |
295 | fdp->fd_ofiles[fd] = NULL; |
296 | while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL) | |
5e00df3b | 297 | fdp->fd_lastfile--; |
8429d022 MK |
298 | if (fd < fdp->fd_freefile) |
299 | fdp->fd_freefile = fd; | |
92438dfc | 300 | *pf = 0; |
8429d022 | 301 | return (closef(fp, p)); |
92438dfc SL |
302 | } |
303 | ||
a53a698b KM |
304 | /* |
305 | * Return status information about a file descriptor. | |
306 | */ | |
307 | /* ARGSUSED */ | |
308 | fstat(p, uap, retval) | |
309 | struct proc *p; | |
310 | register struct args { | |
8429d022 | 311 | int fd; |
92438dfc | 312 | struct stat *sb; |
a53a698b KM |
313 | } *uap; |
314 | int *retval; | |
315 | { | |
5e00df3b | 316 | register struct filedesc *fdp = p->p_fd; |
a53a698b | 317 | register struct file *fp; |
92438dfc | 318 | struct stat ub; |
a53a698b | 319 | int error; |
92438dfc | 320 | |
8429d022 | 321 | if ((unsigned)uap->fd >= fdp->fd_nfiles || |
7ddc4f96 | 322 | (fp = fdp->fd_ofiles[uap->fd]) == NULL) |
d9c2f47f | 323 | return (EBADF); |
92438dfc SL |
324 | switch (fp->f_type) { |
325 | ||
c4ec2128 | 326 | case DTYPE_VNODE: |
208eb71b | 327 | error = vn_stat((struct vnode *)fp->f_data, &ub, p); |
92438dfc SL |
328 | break; |
329 | ||
330 | case DTYPE_SOCKET: | |
a53a698b | 331 | error = soo_stat((struct socket *)fp->f_data, &ub); |
92438dfc SL |
332 | break; |
333 | ||
334 | default: | |
335 | panic("fstat"); | |
336 | /*NOTREACHED*/ | |
337 | } | |
a53a698b KM |
338 | if (error == 0) |
339 | error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub)); | |
d9c2f47f | 340 | return (error); |
3ebb7a40 BJ |
341 | } |
342 | ||
40056b21 | 343 | /* |
8429d022 | 344 | * Allocate a file descriptor for the process. |
40056b21 | 345 | */ |
8429d022 | 346 | int fdexpand; |
5e00df3b | 347 | |
8429d022 MK |
348 | fdalloc(p, want, result) |
349 | struct proc *p; | |
350 | int want; | |
c4ec2128 | 351 | int *result; |
4147b3f6 | 352 | { |
8429d022 MK |
353 | register struct filedesc *fdp = p->p_fd; |
354 | register int i; | |
355 | int lim, last, nfiles; | |
5e00df3b KM |
356 | struct file **newofile; |
357 | char *newofileflags; | |
358 | ||
8429d022 MK |
359 | /* |
360 | * Search for a free descriptor starting at the higher | |
361 | * of want or fd_freefile. If that fails, consider | |
362 | * expanding the ofile array. | |
363 | */ | |
364 | lim = p->p_rlimit[RLIMIT_OFILE].rlim_cur; | |
5e00df3b | 365 | for (;;) { |
8429d022 MK |
366 | last = min(fdp->fd_nfiles, lim); |
367 | if ((i = want) < fdp->fd_freefile) | |
368 | i = fdp->fd_freefile; | |
369 | for (; i < last; i++) { | |
7ddc4f96 MK |
370 | if (fdp->fd_ofiles[i] == NULL) { |
371 | fdp->fd_ofileflags[i] = 0; | |
8429d022 MK |
372 | if (i > fdp->fd_lastfile) |
373 | fdp->fd_lastfile = i; | |
7ddc4f96 | 374 | if (want <= fdp->fd_freefile) |
8429d022 MK |
375 | fdp->fd_freefile = i; |
376 | *result = i; | |
5e00df3b KM |
377 | return (0); |
378 | } | |
379 | } | |
8429d022 MK |
380 | |
381 | /* | |
382 | * No space in current array. Expand? | |
383 | */ | |
384 | if (fdp->fd_nfiles >= lim) | |
5e00df3b | 385 | return (EMFILE); |
7ddc4f96 MK |
386 | if (fdp->fd_nfiles < NDEXTENT) |
387 | nfiles = NDEXTENT; | |
388 | else | |
389 | nfiles = 2 * fdp->fd_nfiles; | |
8429d022 MK |
390 | MALLOC(newofile, struct file **, nfiles * OFILESIZE, |
391 | M_FILEDESC, M_WAITOK); | |
392 | newofileflags = (char *) &newofile[nfiles]; | |
393 | /* | |
394 | * Copy the existing ofile and ofileflags arrays | |
395 | * and zero the new portion of each array. | |
396 | */ | |
397 | bcopy(fdp->fd_ofiles, newofile, | |
398 | (i = sizeof(struct file *) * fdp->fd_nfiles)); | |
399 | bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i); | |
400 | bcopy(fdp->fd_ofileflags, newofileflags, | |
401 | (i = sizeof(char) * fdp->fd_nfiles)); | |
402 | bzero(newofileflags + i, nfiles * sizeof(char) - i); | |
7ddc4f96 MK |
403 | if (fdp->fd_nfiles > NDFILE) |
404 | FREE(fdp->fd_ofiles, M_FILEDESC); | |
8429d022 MK |
405 | fdp->fd_ofiles = newofile; |
406 | fdp->fd_ofileflags = newofileflags; | |
407 | fdp->fd_nfiles = nfiles; | |
408 | fdexpand++; | |
06b22f9b | 409 | } |
4147b3f6 BJ |
410 | } |
411 | ||
a53a698b | 412 | /* |
7ddc4f96 MK |
413 | * Check to see whether n user file descriptors |
414 | * are available to the process p. | |
a53a698b | 415 | */ |
8429d022 MK |
416 | fdavail(p, n) |
417 | struct proc *p; | |
418 | register int n; | |
88a7a62a | 419 | { |
8429d022 | 420 | register struct filedesc *fdp = p->p_fd; |
7ddc4f96 | 421 | register struct file **fpp; |
8429d022 | 422 | register int i; |
88a7a62a | 423 | |
8429d022 MK |
424 | if ((i = p->p_rlimit[RLIMIT_OFILE].rlim_cur - fdp->fd_nfiles) > 0 && |
425 | (n -= i) <= 0) | |
426 | return (1); | |
7ddc4f96 MK |
427 | fpp = &fdp->fd_ofiles[fdp->fd_freefile]; |
428 | for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++) | |
429 | if (*fpp == NULL && --n <= 0) | |
8429d022 MK |
430 | return (1); |
431 | return (0); | |
88a7a62a SL |
432 | } |
433 | ||
4147b3f6 | 434 | /* |
8429d022 MK |
435 | * Create a new open file structure and allocate |
436 | * a file decriptor for the process that refers to it. | |
4147b3f6 | 437 | */ |
5e00df3b KM |
438 | falloc(p, resultfp, resultfd) |
439 | register struct proc *p; | |
c4ec2128 KM |
440 | struct file **resultfp; |
441 | int *resultfd; | |
4147b3f6 | 442 | { |
c753a490 | 443 | register struct file *fp, *fq, **fpp; |
c4ec2128 | 444 | int error, i; |
4147b3f6 | 445 | |
8429d022 | 446 | if (error = fdalloc(p, 0, &i)) |
c4ec2128 | 447 | return (error); |
c753a490 KM |
448 | if (nfiles >= maxfiles) { |
449 | tablefull("file"); | |
450 | return (ENFILE); | |
451 | } | |
452 | /* | |
453 | * Allocate a new file descriptor. | |
454 | * If the process has file descriptor zero open, add to the list | |
455 | * of open files at that point, otherwise put it at the front of | |
456 | * the list of open files. | |
457 | */ | |
458 | nfiles++; | |
459 | MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK); | |
c753a490 KM |
460 | if (fq = p->p_fd->fd_ofiles[0]) |
461 | fpp = &fq->f_filef; | |
462 | else | |
463 | fpp = &filehead; | |
c1884703 | 464 | p->p_fd->fd_ofiles[i] = fp; |
c753a490 KM |
465 | if (fq = *fpp) |
466 | fq->f_fileb = &fp->f_filef; | |
467 | fp->f_filef = fq; | |
468 | fp->f_fileb = fpp; | |
469 | *fpp = fp; | |
88a7a62a | 470 | fp->f_count = 1; |
c753a490 | 471 | fp->f_msgcount = 0; |
4147b3f6 | 472 | fp->f_offset = 0; |
8429d022 | 473 | fp->f_cred = p->p_ucred; |
c4ec2128 | 474 | crhold(fp->f_cred); |
c4ec2128 KM |
475 | if (resultfp) |
476 | *resultfp = fp; | |
477 | if (resultfd) | |
478 | *resultfd = i; | |
479 | return (0); | |
4147b3f6 BJ |
480 | } |
481 | ||
c753a490 KM |
482 | /* |
483 | * Free a file descriptor. | |
484 | */ | |
485 | ffree(fp) | |
486 | register struct file *fp; | |
487 | { | |
488 | register struct file *fq; | |
489 | ||
490 | if (fq = fp->f_filef) | |
491 | fq->f_fileb = fp->f_fileb; | |
492 | *fp->f_fileb = fq; | |
493 | crfree(fp->f_cred); | |
494 | #ifdef DIAGNOSTIC | |
495 | fp->f_filef = NULL; | |
496 | fp->f_fileb = NULL; | |
497 | fp->f_count = 0; | |
498 | #endif | |
499 | nfiles--; | |
500 | FREE(fp, M_FILE); | |
501 | } | |
502 | ||
5e00df3b | 503 | /* |
8429d022 | 504 | * Copy a filedesc structure. |
5e00df3b KM |
505 | */ |
506 | struct filedesc * | |
8429d022 MK |
507 | fdcopy(p) |
508 | struct proc *p; | |
5e00df3b | 509 | { |
7ddc4f96 MK |
510 | register struct filedesc *newfdp, *fdp = p->p_fd; |
511 | register struct file **fpp; | |
5e00df3b | 512 | register int i; |
5e00df3b | 513 | |
7ddc4f96 MK |
514 | MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0), |
515 | M_FILEDESC, M_WAITOK); | |
516 | bcopy(fdp, newfdp, sizeof(struct filedesc)); | |
5e00df3b KM |
517 | VREF(newfdp->fd_cdir); |
518 | if (newfdp->fd_rdir) | |
519 | VREF(newfdp->fd_rdir); | |
520 | newfdp->fd_refcnt = 1; | |
8429d022 MK |
521 | |
522 | /* | |
7ddc4f96 MK |
523 | * If the number of open files fits in the internal arrays |
524 | * of the open file structure, use them, otherwise allocate | |
525 | * additional memory for the number of descriptors currently | |
526 | * in use. | |
8429d022 | 527 | */ |
7ddc4f96 MK |
528 | if (newfdp->fd_lastfile < NDFILE) { |
529 | newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles; | |
530 | newfdp->fd_ofileflags = | |
531 | ((struct filedesc0 *) newfdp)->fd_dfileflags; | |
532 | i = NDFILE; | |
533 | } else { | |
534 | /* | |
535 | * Compute the smallest multiple of NDEXTENT needed | |
536 | * for the file descriptors currently in use, | |
537 | * allowing the table to shrink. | |
538 | */ | |
539 | i = newfdp->fd_nfiles; | |
540 | while (i > 2 * NDEXTENT && i >= newfdp->fd_lastfile * 2) | |
541 | i /= 2; | |
542 | MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE, | |
543 | M_FILEDESC, M_WAITOK); | |
544 | newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i]; | |
545 | } | |
8429d022 | 546 | newfdp->fd_nfiles = i; |
8429d022 MK |
547 | bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **)); |
548 | bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char)); | |
7ddc4f96 MK |
549 | fpp = newfdp->fd_ofiles; |
550 | for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) | |
551 | if (*fpp != NULL) | |
552 | (*fpp)->f_count++; | |
5e00df3b KM |
553 | return (newfdp); |
554 | } | |
555 | ||
556 | /* | |
557 | * Release a filedesc structure. | |
558 | */ | |
7ddc4f96 | 559 | void |
8429d022 MK |
560 | fdfree(p) |
561 | struct proc *p; | |
5e00df3b | 562 | { |
8429d022 | 563 | register struct filedesc *fdp = p->p_fd; |
7ddc4f96 | 564 | struct file **fpp; |
5e00df3b KM |
565 | register int i; |
566 | ||
8429d022 | 567 | if (--fdp->fd_refcnt > 0) |
5e00df3b | 568 | return; |
7ddc4f96 MK |
569 | fpp = fdp->fd_ofiles; |
570 | for (i = fdp->fd_lastfile; i-- >= 0; fpp++) | |
571 | if (*fpp) | |
572 | (void) closef(*fpp, p); | |
573 | if (fdp->fd_nfiles > NDFILE) | |
574 | FREE(fdp->fd_ofiles, M_FILEDESC); | |
5e00df3b KM |
575 | vrele(fdp->fd_cdir); |
576 | if (fdp->fd_rdir) | |
577 | vrele(fdp->fd_rdir); | |
8429d022 | 578 | FREE(fdp, M_FILEDESC); |
5e00df3b KM |
579 | } |
580 | ||
4147b3f6 BJ |
581 | /* |
582 | * Internal form of close. | |
88a7a62a | 583 | * Decrement reference count on file structure. |
4147b3f6 | 584 | */ |
8429d022 | 585 | closef(fp, p) |
4147b3f6 | 586 | register struct file *fp; |
c753a490 | 587 | register struct proc *p; |
4147b3f6 | 588 | { |
a4128336 KM |
589 | struct vnode *vp; |
590 | struct flock lf; | |
f9934296 | 591 | int error; |
4147b3f6 | 592 | |
40056b21 | 593 | if (fp == NULL) |
f9934296 | 594 | return (0); |
a4128336 KM |
595 | /* |
596 | * POSIX record locking dictates that any close releases ALL | |
597 | * locks owned by this process. This is handled by setting | |
598 | * a flag in the unlock to free ONLY locks obeying POSIX | |
599 | * semantics, and not to free BSD-style file locks. | |
600 | */ | |
9bd5ee04 | 601 | if ((p->p_flag & SADVLCK) && fp->f_type == DTYPE_VNODE) { |
a4128336 KM |
602 | lf.l_whence = SEEK_SET; |
603 | lf.l_start = 0; | |
604 | lf.l_len = 0; | |
605 | lf.l_type = F_UNLCK; | |
606 | vp = (struct vnode *)fp->f_data; | |
208eb71b | 607 | (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX); |
a4128336 | 608 | } |
8429d022 | 609 | if (--fp->f_count > 0) |
f9934296 | 610 | return (0); |
8429d022 MK |
611 | if (fp->f_count < 0) |
612 | panic("closef: count < 0"); | |
9bd5ee04 KM |
613 | if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) { |
614 | lf.l_whence = SEEK_SET; | |
615 | lf.l_start = 0; | |
616 | lf.l_len = 0; | |
617 | lf.l_type = F_UNLCK; | |
618 | vp = (struct vnode *)fp->f_data; | |
208eb71b | 619 | (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); |
9bd5ee04 | 620 | } |
208eb71b | 621 | error = (*fp->f_ops->fo_close)(fp, p); |
c753a490 | 622 | ffree(fp); |
f9934296 | 623 | return (error); |
8d528261 | 624 | } |
92438dfc SL |
625 | |
626 | /* | |
627 | * Apply an advisory lock on a file descriptor. | |
a4128336 KM |
628 | * |
629 | * Just attempt to get a record lock of the requested type on | |
630 | * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0). | |
92438dfc | 631 | */ |
a4128336 | 632 | |
a53a698b KM |
633 | /* ARGSUSED */ |
634 | flock(p, uap, retval) | |
635 | struct proc *p; | |
636 | register struct args { | |
8429d022 | 637 | int fd; |
92438dfc | 638 | int how; |
a53a698b KM |
639 | } *uap; |
640 | int *retval; | |
641 | { | |
5e00df3b | 642 | register struct filedesc *fdp = p->p_fd; |
92438dfc | 643 | register struct file *fp; |
a4128336 KM |
644 | struct vnode *vp; |
645 | struct flock lf; | |
646 | int error; | |
92438dfc | 647 | |
8429d022 | 648 | if ((unsigned)uap->fd >= fdp->fd_nfiles || |
7ddc4f96 | 649 | (fp = fdp->fd_ofiles[uap->fd]) == NULL) |
d9c2f47f | 650 | return (EBADF); |
a53a698b | 651 | if (fp->f_type != DTYPE_VNODE) |
d9c2f47f | 652 | return (EOPNOTSUPP); |
a4128336 KM |
653 | vp = (struct vnode *)fp->f_data; |
654 | lf.l_whence = SEEK_SET; | |
655 | lf.l_start = 0; | |
656 | lf.l_len = 0; | |
55dfda24 | 657 | if (uap->how & LOCK_UN) { |
a4128336 | 658 | lf.l_type = F_UNLCK; |
9bd5ee04 | 659 | fp->f_flag &= ~FHASLOCK; |
208eb71b | 660 | return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK)); |
92438dfc | 661 | } |
742253b3 | 662 | if (uap->how & LOCK_EX) |
a4128336 KM |
663 | lf.l_type = F_WRLCK; |
664 | else if (uap->how & LOCK_SH) | |
665 | lf.l_type = F_RDLCK; | |
666 | else | |
667 | return (EBADF); | |
9bd5ee04 | 668 | fp->f_flag |= FHASLOCK; |
a4128336 | 669 | if (uap->how & LOCK_NB) |
208eb71b KM |
670 | return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK)); |
671 | return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT)); | |
92438dfc | 672 | } |
75ebd8c9 MT |
673 | |
674 | /* | |
675 | * File Descriptor pseudo-device driver (/dev/fd/). | |
676 | * | |
75ebd8c9 MT |
677 | * Opening minor device N dup()s the file (if any) connected to file |
678 | * descriptor N belonging to the calling process. Note that this driver | |
679 | * consists of only the ``open()'' routine, because all subsequent | |
680 | * references to this file will be direct to the other driver. | |
681 | */ | |
c4ec2128 KM |
682 | /* ARGSUSED */ |
683 | fdopen(dev, mode, type) | |
75ebd8c9 | 684 | dev_t dev; |
c4ec2128 | 685 | int mode, type; |
75ebd8c9 | 686 | { |
75ebd8c9 MT |
687 | |
688 | /* | |
8429d022 | 689 | * XXX Kludge: set curproc->p_dupfd to contain the value of the |
1105efe2 KM |
690 | * the file descriptor being sought for duplication. The error |
691 | * return ensures that the vnode for this device will be released | |
692 | * by vn_open. Open will detect this special error and take the | |
693 | * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN | |
694 | * will simply report the error. | |
75ebd8c9 | 695 | */ |
8429d022 | 696 | curproc->p_dupfd = minor(dev); /* XXX */ |
1105efe2 KM |
697 | return (ENODEV); |
698 | } | |
e99d5db8 | 699 | |
1105efe2 KM |
700 | /* |
701 | * Duplicate the specified descriptor to a free descriptor. | |
702 | */ | |
5e00df3b KM |
703 | dupfdopen(fdp, indx, dfd, mode) |
704 | register struct filedesc *fdp; | |
1105efe2 KM |
705 | register int indx, dfd; |
706 | int mode; | |
707 | { | |
708 | register struct file *wfp; | |
709 | struct file *fp; | |
710 | ||
75ebd8c9 | 711 | /* |
1105efe2 KM |
712 | * If the to-be-dup'd fd number is greater than the allowed number |
713 | * of file descriptors, or the fd to be dup'd has already been | |
714 | * closed, reject. Note, check for new == old is necessary as | |
715 | * falloc could allocate an already closed to-be-dup'd descriptor | |
716 | * as the new descriptor. | |
75ebd8c9 | 717 | */ |
7ddc4f96 MK |
718 | fp = fdp->fd_ofiles[indx]; |
719 | if ((u_int)dfd >= fdp->fd_nfiles || | |
720 | (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp) | |
75ebd8c9 | 721 | return (EBADF); |
e99d5db8 | 722 | |
75ebd8c9 | 723 | /* |
e99d5db8 KB |
724 | * Check that the mode the file is being opened for is a subset |
725 | * of the mode of the existing descriptor. | |
75ebd8c9 | 726 | */ |
1105efe2 | 727 | if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) |
75ebd8c9 | 728 | return (EACCES); |
7ddc4f96 MK |
729 | fdp->fd_ofiles[indx] = wfp; |
730 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; | |
a53a698b | 731 | wfp->f_count++; |
5e00df3b KM |
732 | if (indx > fdp->fd_lastfile) |
733 | fdp->fd_lastfile = indx; | |
1105efe2 | 734 | return (0); |
75ebd8c9 | 735 | } |