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