]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
9bccf70c | 2 | * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
de355530 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
de355530 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
de355530 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
23 | /* | |
24 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 | |
25 | * The Regents of the University of California. All rights reserved. | |
26 | * (c) UNIX System Laboratories, Inc. | |
27 | * All or some portions of this file are derived from material licensed | |
28 | * to the University of California by American Telephone and Telegraph | |
29 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
30 | * the permission of UNIX System Laboratories, Inc. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95 | |
1c79356b A |
61 | */ |
62 | ||
63 | #include <sys/param.h> | |
64 | #include <sys/systm.h> | |
65 | #include <sys/filedesc.h> | |
66 | #include <sys/kernel.h> | |
67 | #include <sys/vnode.h> | |
68 | #include <sys/proc.h> | |
69 | #include <sys/file.h> | |
70 | #include <sys/socket.h> | |
71 | #include <sys/socketvar.h> | |
72 | #include <sys/stat.h> | |
73 | #include <sys/ioctl.h> | |
74 | #include <sys/fcntl.h> | |
75 | #include <sys/malloc.h> | |
76 | #include <sys/syslog.h> | |
77 | #include <sys/unistd.h> | |
78 | #include <sys/resourcevar.h> | |
79 | ||
80 | #include <sys/mount.h> | |
81 | ||
82 | /* | |
83 | * Descriptor management. | |
84 | */ | |
85 | struct filelist filehead; /* head of list of open files */ | |
86 | int nfiles; /* actual number of open files */ | |
87 | ||
9bccf70c A |
88 | static int frele_internal(struct file *); |
89 | ||
1c79356b A |
90 | /* |
91 | * System calls on descriptors. | |
92 | */ | |
93 | /* ARGSUSED */ | |
94 | int | |
95 | getdtablesize(p, uap, retval) | |
96 | struct proc *p; | |
97 | void *uap; | |
98 | register_t *retval; | |
99 | { | |
1c79356b A |
100 | *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); |
101 | return (0); | |
102 | } | |
103 | ||
104 | /* ARGSUSED */ | |
105 | int | |
106 | ogetdtablesize(p, uap, retval) | |
107 | struct proc *p; | |
108 | void *uap; | |
109 | register_t *retval; | |
110 | { | |
1c79356b A |
111 | *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, NOFILE); |
112 | return (0); | |
113 | } | |
114 | ||
115 | static __inline__ | |
116 | void _fdrelse(fdp, fd) | |
117 | register struct filedesc *fdp; | |
118 | register int fd; | |
119 | { | |
120 | if (fd < fdp->fd_freefile) | |
121 | fdp->fd_freefile = fd; | |
122 | #if DIAGNOSTIC | |
123 | if (fd > fdp->fd_lastfile) | |
124 | panic("fdrelse: fd_lastfile inconsistent"); | |
125 | #endif | |
126 | fdp->fd_ofiles[fd] = NULL; | |
127 | fdp->fd_ofileflags[fd] = 0; | |
128 | while ((fd = fdp->fd_lastfile) > 0 && | |
129 | fdp->fd_ofiles[fd] == NULL && | |
130 | !(fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
131 | fdp->fd_lastfile--; | |
132 | } | |
133 | ||
134 | /* | |
135 | * Duplicate a file descriptor. | |
136 | */ | |
137 | struct dup_args { | |
138 | u_int fd; | |
139 | }; | |
140 | /* ARGSUSED */ | |
141 | int | |
142 | dup(p, uap, retval) | |
143 | struct proc *p; | |
144 | struct dup_args *uap; | |
145 | register_t *retval; | |
146 | { | |
147 | register struct filedesc *fdp = p->p_fd; | |
148 | register int old = uap->fd; | |
149 | int new, error; | |
150 | ||
151 | if ((u_int)old >= fdp->fd_nfiles || | |
152 | fdp->fd_ofiles[old] == NULL || | |
153 | (fdp->fd_ofileflags[old] & UF_RESERVED)) | |
154 | return (EBADF); | |
155 | if (error = fdalloc(p, 0, &new)) | |
156 | return (error); | |
157 | return (finishdup(fdp, old, new, retval)); | |
158 | } | |
159 | ||
160 | /* | |
161 | * Duplicate a file descriptor to a particular value. | |
162 | */ | |
163 | struct dup2_args { | |
164 | u_int from; | |
165 | u_int to; | |
166 | }; | |
167 | /* ARGSUSED */ | |
168 | int | |
169 | dup2(p, uap, retval) | |
170 | struct proc *p; | |
171 | struct dup2_args *uap; | |
172 | register_t *retval; | |
173 | { | |
174 | register struct filedesc *fdp = p->p_fd; | |
175 | register int old = uap->from, new = uap->to; | |
176 | int i, error; | |
177 | ||
178 | if ((u_int)old >= fdp->fd_nfiles || | |
179 | fdp->fd_ofiles[old] == NULL || | |
180 | (fdp->fd_ofileflags[old] & UF_RESERVED) || | |
181 | (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || | |
182 | (u_int)new >= maxfiles) | |
183 | return (EBADF); | |
184 | if (old == new) { | |
185 | *retval = new; | |
186 | return (0); | |
187 | } | |
188 | if ((u_int)new >= fdp->fd_nfiles) { | |
189 | if (error = fdalloc(p, new, &i)) | |
190 | return (error); | |
191 | if (new != i) { | |
192 | _fdrelse(fdp, i); | |
193 | goto closeit; | |
194 | } | |
fa4905b1 | 195 | } else { |
1c79356b A |
196 | struct file **fpp; |
197 | char flags; | |
198 | closeit: | |
199 | if ((flags = fdp->fd_ofileflags[new]) & UF_RESERVED) | |
200 | return (EBADF); | |
201 | fdp->fd_ofileflags[new] = (flags & ~UF_MAPPED) | UF_RESERVED; | |
202 | /* | |
203 | * dup2() must succeed even if the close has an error. | |
204 | */ | |
205 | if (*(fpp = &fdp->fd_ofiles[new])) { | |
206 | struct file *fp = *fpp; | |
207 | ||
fa4905b1 A |
208 | *fpp = NULL; |
209 | (void) closef(fp, p); | |
1c79356b A |
210 | } |
211 | } | |
212 | return (finishdup(fdp, old, new, retval)); | |
213 | } | |
214 | ||
215 | /* | |
216 | * The file control system call. | |
217 | */ | |
218 | struct fcntl_args { | |
219 | int fd; | |
220 | int cmd; | |
221 | int arg; | |
222 | }; | |
223 | /* ARGSUSED */ | |
224 | int | |
225 | fcntl(p, uap, retval) | |
226 | struct proc *p; | |
227 | register struct fcntl_args *uap; | |
228 | register_t *retval; | |
229 | { | |
230 | int fd = uap->fd; | |
231 | register struct filedesc *fdp = p->p_fd; | |
232 | register struct file *fp; | |
233 | register char *pop; | |
234 | struct vnode *vp, *devvp; | |
235 | int i, tmp, error, error2, flg = F_POSIX; | |
236 | struct flock fl; | |
9bccf70c | 237 | fstore_t alloc_struct; /* structure for allocate command */ |
1c79356b A |
238 | u_int32_t alloc_flags = 0; |
239 | off_t offset; /* used for F_SETSIZE */ | |
240 | int newmin; | |
241 | struct radvisory ra_struct; | |
242 | fbootstraptransfer_t fbt_struct; /* for F_READBOOTSTRAP and F_WRITEBOOTSTRAP */ | |
9bccf70c | 243 | struct log2phys l2p_struct; /* structure for allocate command */ |
1c79356b A |
244 | daddr_t lbn, bn; |
245 | int devBlockSize = 0; | |
246 | ||
247 | if ((u_int)fd >= fdp->fd_nfiles || | |
248 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
249 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
250 | return (EBADF); | |
251 | pop = &fdp->fd_ofileflags[fd]; | |
252 | switch (uap->cmd) { | |
253 | ||
254 | case F_DUPFD: | |
255 | newmin = (long)uap->arg; | |
256 | if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || | |
257 | (u_int)newmin >= maxfiles) | |
258 | return (EINVAL); | |
259 | if (error = fdalloc(p, newmin, &i)) | |
260 | return (error); | |
261 | return (finishdup(fdp, fd, i, retval)); | |
262 | ||
263 | case F_GETFD: | |
264 | *retval = (*pop & UF_EXCLOSE)? 1 : 0; | |
265 | return (0); | |
266 | ||
267 | case F_SETFD: | |
268 | *pop = (*pop &~ UF_EXCLOSE) | | |
269 | ((long)(uap->arg) & 1)? UF_EXCLOSE : 0; | |
270 | return (0); | |
271 | ||
272 | case F_GETFL: | |
273 | *retval = OFLAGS(fp->f_flag); | |
274 | return (0); | |
275 | ||
276 | case F_SETFL: | |
277 | fp->f_flag &= ~FCNTLFLAGS; | |
278 | fp->f_flag |= FFLAGS((long)uap->arg) & FCNTLFLAGS; | |
279 | tmp = fp->f_flag & FNONBLOCK; | |
9bccf70c | 280 | error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p); |
1c79356b A |
281 | if (error) |
282 | return (error); | |
283 | tmp = fp->f_flag & FASYNC; | |
9bccf70c | 284 | error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p); |
1c79356b A |
285 | if (!error) |
286 | return (0); | |
287 | fp->f_flag &= ~FNONBLOCK; | |
288 | tmp = 0; | |
9bccf70c | 289 | (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p); |
1c79356b A |
290 | return (error); |
291 | ||
292 | case F_GETOWN: | |
293 | if (fp->f_type == DTYPE_SOCKET) { | |
294 | *retval = ((struct socket *)fp->f_data)->so_pgid; | |
295 | return (0); | |
296 | } | |
9bccf70c | 297 | error = fo_ioctl(fp, (int)TIOCGPGRP, (caddr_t)retval, p); |
1c79356b A |
298 | *retval = -*retval; |
299 | return (error); | |
300 | ||
301 | case F_SETOWN: | |
302 | if (fp->f_type == DTYPE_SOCKET) { | |
303 | ((struct socket *)fp->f_data)->so_pgid = | |
304 | (long)uap->arg; | |
305 | return (0); | |
306 | } | |
307 | if ((long)uap->arg <= 0) { | |
d7e50217 | 308 | uap->arg = (int)(-(long)(uap->arg)); |
1c79356b A |
309 | } else { |
310 | struct proc *p1 = pfind((long)uap->arg); | |
311 | if (p1 == 0) | |
312 | return (ESRCH); | |
d7e50217 | 313 | uap->arg = (int)p1->p_pgrp->pg_id; |
1c79356b | 314 | } |
9bccf70c | 315 | return (fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p)); |
1c79356b A |
316 | |
317 | case F_SETLKW: | |
318 | flg |= F_WAIT; | |
319 | /* Fall into F_SETLK */ | |
320 | ||
321 | case F_SETLK: | |
322 | if (fp->f_type != DTYPE_VNODE) | |
323 | return (EBADF); | |
324 | vp = (struct vnode *)fp->f_data; | |
325 | /* Copy in the lock structure */ | |
326 | error = copyin((caddr_t)uap->arg, (caddr_t)&fl, | |
327 | sizeof (fl)); | |
328 | if (error) | |
329 | return (error); | |
330 | if (fl.l_whence == SEEK_CUR) | |
331 | fl.l_start += fp->f_offset; | |
332 | switch (fl.l_type) { | |
333 | ||
334 | case F_RDLCK: | |
335 | if ((fp->f_flag & FREAD) == 0) | |
336 | return (EBADF); | |
337 | p->p_flag |= P_ADVLOCK; | |
338 | return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg)); | |
339 | ||
340 | case F_WRLCK: | |
341 | if ((fp->f_flag & FWRITE) == 0) | |
342 | return (EBADF); | |
343 | p->p_flag |= P_ADVLOCK; | |
344 | return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg)); | |
345 | ||
346 | case F_UNLCK: | |
347 | return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl, | |
348 | F_POSIX)); | |
349 | ||
350 | default: | |
351 | return (EINVAL); | |
352 | } | |
353 | ||
354 | case F_GETLK: | |
355 | if (fp->f_type != DTYPE_VNODE) | |
356 | return (EBADF); | |
357 | vp = (struct vnode *)fp->f_data; | |
358 | /* Copy in the lock structure */ | |
359 | error = copyin((caddr_t)uap->arg, (caddr_t)&fl, | |
360 | sizeof (fl)); | |
361 | if (error) | |
362 | return (error); | |
363 | if (fl.l_whence == SEEK_CUR) | |
364 | fl.l_start += fp->f_offset; | |
365 | if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX)) | |
366 | return (error); | |
367 | return (copyout((caddr_t)&fl, (caddr_t)uap->arg, | |
368 | sizeof (fl))); | |
369 | ||
9bccf70c A |
370 | case F_PREALLOCATE: |
371 | if (fp->f_type != DTYPE_VNODE) | |
372 | return (EBADF); | |
373 | ||
374 | /* make sure that we have write permission */ | |
375 | if ((fp->f_flag & FWRITE) == 0) | |
376 | return (EBADF); | |
1c79356b A |
377 | |
378 | error = copyin((caddr_t)uap->arg, (caddr_t)&alloc_struct, | |
379 | sizeof (alloc_struct)); | |
1c79356b A |
380 | if (error) |
381 | return (error); | |
382 | ||
9bccf70c | 383 | /* now set the space allocated to 0 */ |
1c79356b A |
384 | alloc_struct.fst_bytesalloc = 0; |
385 | ||
9bccf70c A |
386 | /* |
387 | * Do some simple parameter checking | |
388 | */ | |
1c79356b A |
389 | |
390 | /* set up the flags */ | |
391 | ||
392 | alloc_flags |= PREALLOCATE; | |
393 | ||
9bccf70c | 394 | if (alloc_struct.fst_flags & F_ALLOCATECONTIG) |
1c79356b | 395 | alloc_flags |= ALLOCATECONTIG; |
1c79356b | 396 | |
9bccf70c A |
397 | if (alloc_struct.fst_flags & F_ALLOCATEALL) |
398 | alloc_flags |= ALLOCATEALL; | |
1c79356b | 399 | |
9bccf70c A |
400 | /* |
401 | * Do any position mode specific stuff. The only | |
402 | * position mode supported now is PEOFPOSMODE | |
403 | */ | |
1c79356b A |
404 | |
405 | switch (alloc_struct.fst_posmode) { | |
406 | ||
407 | case F_PEOFPOSMODE: | |
0b4e3aa0 A |
408 | if (alloc_struct.fst_offset != 0) |
409 | return (EINVAL); | |
1c79356b A |
410 | |
411 | alloc_flags |= ALLOCATEFROMPEOF; | |
412 | break; | |
413 | ||
0b4e3aa0 | 414 | case F_VOLPOSMODE: |
0b4e3aa0 A |
415 | if (alloc_struct.fst_offset <= 0) |
416 | return (EINVAL); | |
417 | ||
418 | alloc_flags |= ALLOCATEFROMVOL; | |
419 | break; | |
420 | ||
1c79356b | 421 | default: |
1c79356b | 422 | return(EINVAL); |
1c79356b A |
423 | } |
424 | ||
9bccf70c | 425 | vp = (struct vnode *)fp->f_data; |
1c79356b | 426 | |
9bccf70c A |
427 | /* lock the vnode and call allocate to get the space */ |
428 | error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p); | |
429 | if (error) | |
430 | return (error); | |
1c79356b | 431 | error = VOP_ALLOCATE(vp,alloc_struct.fst_length,alloc_flags, |
0b4e3aa0 A |
432 | &alloc_struct.fst_bytesalloc, alloc_struct.fst_offset, |
433 | fp->f_cred, p); | |
9bccf70c | 434 | VOP_UNLOCK(vp, 0, p); |
1c79356b | 435 | |
9bccf70c A |
436 | if (error2 = copyout((caddr_t)&alloc_struct, |
437 | (caddr_t)uap->arg, | |
438 | sizeof (alloc_struct))) { | |
439 | if (error) | |
1c79356b | 440 | return(error); |
9bccf70c | 441 | else |
1c79356b | 442 | return(error2); |
1c79356b | 443 | } |
1c79356b A |
444 | return(error); |
445 | ||
9bccf70c A |
446 | case F_SETSIZE: |
447 | if (fp->f_type != DTYPE_VNODE) | |
448 | return (EBADF); | |
1c79356b | 449 | |
1c79356b | 450 | error = copyin((caddr_t)uap->arg, (caddr_t)&offset, |
9bccf70c | 451 | sizeof (off_t)); |
1c79356b A |
452 | if (error) |
453 | return (error); | |
454 | ||
9bccf70c A |
455 | /* |
456 | * Make sure that we are root. Growing a file | |
457 | * without zero filling the data is a security hole | |
458 | * root would have access anyway so we'll allow it | |
459 | */ | |
1c79356b | 460 | |
9bccf70c | 461 | if (!is_suser()) |
1c79356b | 462 | return (EACCES); |
1c79356b | 463 | |
9bccf70c | 464 | vp = (struct vnode *)fp->f_data; |
1c79356b | 465 | |
9bccf70c A |
466 | /* lock the vnode and call allocate to get the space */ |
467 | error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p); | |
468 | if (error) | |
469 | return (error); | |
1c79356b A |
470 | error = VOP_TRUNCATE(vp,offset,IO_NOZEROFILL,fp->f_cred,p); |
471 | VOP_UNLOCK(vp,0,p); | |
1c79356b | 472 | return(error); |
9bccf70c A |
473 | |
474 | case F_RDAHEAD: | |
475 | if (fp->f_type != DTYPE_VNODE) | |
476 | return (EBADF); | |
477 | vp = (struct vnode *)fp->f_data; | |
478 | ||
1c79356b A |
479 | simple_lock(&vp->v_interlock); |
480 | if (uap->arg) | |
9bccf70c | 481 | vp->v_flag &= ~VRAOFF; |
1c79356b | 482 | else |
9bccf70c | 483 | vp->v_flag |= VRAOFF; |
1c79356b | 484 | simple_unlock(&vp->v_interlock); |
1c79356b A |
485 | return (0); |
486 | ||
9bccf70c A |
487 | case F_NOCACHE: |
488 | if (fp->f_type != DTYPE_VNODE) | |
489 | return (EBADF); | |
490 | vp = (struct vnode *)fp->f_data; | |
491 | ||
1c79356b A |
492 | simple_lock(&vp->v_interlock); |
493 | if (uap->arg) | |
9bccf70c | 494 | vp->v_flag |= VNOCACHE_DATA; |
1c79356b | 495 | else |
9bccf70c | 496 | vp->v_flag &= ~VNOCACHE_DATA; |
1c79356b | 497 | simple_unlock(&vp->v_interlock); |
1c79356b A |
498 | return (0); |
499 | ||
500 | case F_RDADVISE: | |
9bccf70c A |
501 | if (fp->f_type != DTYPE_VNODE) |
502 | return (EBADF); | |
503 | vp = (struct vnode *)fp->f_data; | |
1c79356b | 504 | |
9bccf70c A |
505 | if (error = copyin((caddr_t)uap->arg, |
506 | (caddr_t)&ra_struct, sizeof (ra_struct))) | |
507 | return(error); | |
d7e50217 | 508 | return (VOP_IOCTL(vp, 1, (caddr_t)&ra_struct, 0, fp->f_cred, p)); |
9bccf70c | 509 | |
1c79356b A |
510 | case F_READBOOTSTRAP: |
511 | case F_WRITEBOOTSTRAP: | |
9bccf70c A |
512 | if (fp->f_type != DTYPE_VNODE) |
513 | return (EBADF); | |
1c79356b A |
514 | |
515 | error = copyin((caddr_t)uap->arg, (caddr_t)&fbt_struct, | |
9bccf70c | 516 | sizeof (fbt_struct)); |
1c79356b A |
517 | if (error) |
518 | return (error); | |
519 | ||
1c79356b | 520 | if (uap->cmd == F_WRITEBOOTSTRAP) { |
9bccf70c A |
521 | /* |
522 | * Make sure that we are root. Updating the | |
523 | * bootstrap on a disk could be a security hole | |
524 | */ | |
525 | if (!is_suser()) | |
526 | return (EACCES); | |
527 | } | |
1c79356b | 528 | |
9bccf70c A |
529 | vp = (struct vnode *)fp->f_data; |
530 | if (vp->v_tag != VT_HFS) /* XXX */ | |
531 | error = EINVAL; | |
532 | else { | |
533 | /* lock the vnode and call VOP_IOCTL to handle the I/O */ | |
534 | error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p); | |
535 | if (error) | |
536 | return (error); | |
537 | error = VOP_IOCTL(vp, (uap->cmd == F_WRITEBOOTSTRAP) ? 3 : 2, | |
d7e50217 | 538 | (caddr_t)&fbt_struct, 0, fp->f_cred, p); |
9bccf70c A |
539 | VOP_UNLOCK(vp,0,p); |
540 | } | |
1c79356b | 541 | return(error); |
9bccf70c A |
542 | |
543 | case F_LOG2PHYS: | |
1c79356b A |
544 | if (fp->f_type != DTYPE_VNODE) |
545 | return (EBADF); | |
546 | vp = (struct vnode *)fp->f_data; | |
9bccf70c A |
547 | error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p); |
548 | if (error) | |
549 | return (error); | |
1c79356b A |
550 | if (VOP_OFFTOBLK(vp, fp->f_offset, &lbn)) |
551 | panic("fcntl LOG2PHYS OFFTOBLK"); | |
552 | if (VOP_BLKTOOFF(vp, lbn, &offset)) | |
553 | panic("fcntl LOG2PHYS BLKTOOFF1"); | |
554 | error = VOP_BMAP(vp, lbn, &devvp, &bn, 0); | |
555 | VOP_DEVBLOCKSIZE(devvp, &devBlockSize); | |
556 | VOP_UNLOCK(vp, 0, p); | |
557 | if (!error) { | |
558 | l2p_struct.l2p_flags = 0; /* for now */ | |
559 | l2p_struct.l2p_contigbytes = 0; /* for now */ | |
560 | l2p_struct.l2p_devoffset = bn * devBlockSize; | |
561 | l2p_struct.l2p_devoffset += fp->f_offset - offset; | |
562 | error = copyout((caddr_t)&l2p_struct, | |
563 | (caddr_t)uap->arg, | |
564 | sizeof (l2p_struct)); | |
565 | } | |
566 | return (error); | |
567 | ||
1c79356b A |
568 | default: |
569 | return (EINVAL); | |
570 | } | |
571 | /* NOTREACHED */ | |
572 | } | |
573 | ||
574 | /* | |
575 | * Common code for dup, dup2, and fcntl(F_DUPFD). | |
576 | */ | |
577 | int | |
578 | finishdup(fdp, old, new, retval) | |
579 | register struct filedesc *fdp; | |
580 | register int old, new; | |
581 | register_t *retval; | |
582 | { | |
583 | register struct file *fp; | |
584 | ||
585 | if ((fp = fdp->fd_ofiles[old]) == NULL || | |
586 | (fdp->fd_ofileflags[old] & UF_RESERVED)) { | |
587 | _fdrelse(fdp, new); | |
588 | return (EBADF); | |
589 | } | |
590 | fdp->fd_ofiles[new] = fp; | |
591 | fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; | |
592 | (void)fref(fp); | |
593 | if (new > fdp->fd_lastfile) | |
594 | fdp->fd_lastfile = new; | |
595 | *retval = new; | |
596 | return (0); | |
597 | } | |
598 | ||
599 | /* | |
600 | * Close a file descriptor. | |
601 | */ | |
602 | struct close_args { | |
603 | int fd; | |
604 | }; | |
605 | /* ARGSUSED */ | |
606 | int | |
607 | close(p, uap, retval) | |
608 | struct proc *p; | |
609 | struct close_args *uap; | |
610 | register_t *retval; | |
611 | { | |
612 | int fd = uap->fd; | |
613 | register struct filedesc *fdp = p->p_fd; | |
614 | register struct file *fp; | |
615 | ||
616 | if ((u_int)fd >= fdp->fd_nfiles || | |
617 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
618 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
619 | return (EBADF); | |
620 | _fdrelse(fdp, fd); | |
621 | return (closef(fp, p)); | |
622 | } | |
623 | ||
624 | /* | |
625 | * Return status information about a file descriptor. | |
626 | */ | |
627 | struct fstat_args { | |
628 | int fd; | |
629 | struct stat *sb; | |
630 | }; | |
631 | /* ARGSUSED */ | |
632 | int | |
633 | fstat(p, uap, retval) | |
634 | struct proc *p; | |
635 | register struct fstat_args *uap; | |
636 | register_t *retval; | |
637 | { | |
638 | int fd = uap->fd; | |
639 | register struct filedesc *fdp = p->p_fd; | |
640 | register struct file *fp; | |
641 | struct stat ub; | |
642 | int error; | |
643 | ||
644 | if ((u_int)fd >= fdp->fd_nfiles || | |
645 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
646 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
647 | return (EBADF); | |
648 | switch (fp->f_type) { | |
649 | ||
650 | case DTYPE_VNODE: | |
651 | error = vn_stat((struct vnode *)fp->f_data, &ub, p); | |
652 | break; | |
653 | ||
654 | case DTYPE_SOCKET: | |
655 | error = soo_stat((struct socket *)fp->f_data, &ub); | |
656 | break; | |
657 | ||
658 | case DTYPE_PSXSHM: | |
659 | error = pshm_stat((void *)fp->f_data, &ub); | |
660 | break; | |
661 | default: | |
662 | panic("fstat"); | |
663 | /*NOTREACHED*/ | |
664 | } | |
665 | if (error == 0) | |
666 | error = copyout((caddr_t)&ub, (caddr_t)uap->sb, | |
667 | sizeof (ub)); | |
668 | return (error); | |
669 | } | |
670 | ||
671 | #if COMPAT_43 | |
672 | /* | |
673 | * Return status information about a file descriptor. | |
674 | */ | |
675 | struct ofstat_args { | |
676 | int fd; | |
677 | struct ostat *sb; | |
678 | }; | |
679 | /* ARGSUSED */ | |
680 | ofstat(p, uap, retval) | |
681 | struct proc *p; | |
682 | register struct ofstat_args *uap; | |
683 | register_t *retval; | |
684 | { | |
685 | int fd = uap->fd; | |
686 | register struct filedesc *fdp = p->p_fd; | |
687 | register struct file *fp; | |
688 | struct stat ub; | |
689 | struct ostat oub; | |
690 | int error; | |
691 | ||
692 | if ((u_int)fd >= fdp->fd_nfiles || | |
693 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
694 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
695 | return (EBADF); | |
696 | switch (fp->f_type) { | |
697 | ||
698 | case DTYPE_VNODE: | |
699 | error = vn_stat((struct vnode *)fp->f_data, &ub, p); | |
700 | break; | |
701 | ||
702 | case DTYPE_SOCKET: | |
703 | error = soo_stat((struct socket *)fp->f_data, &ub); | |
704 | break; | |
705 | ||
706 | default: | |
707 | panic("ofstat"); | |
708 | /*NOTREACHED*/ | |
709 | } | |
710 | cvtstat(&ub, &oub); | |
711 | if (error == 0) | |
712 | error = copyout((caddr_t)&oub, (caddr_t)uap->sb, | |
713 | sizeof (oub)); | |
714 | return (error); | |
715 | } | |
716 | #endif /* COMPAT_43 */ | |
717 | ||
718 | /* | |
719 | * Return pathconf information about a file descriptor. | |
720 | */ | |
721 | struct fpathconf_args { | |
722 | int fd; | |
723 | int name; | |
724 | }; | |
725 | /* ARGSUSED */ | |
726 | fpathconf(p, uap, retval) | |
727 | struct proc *p; | |
728 | register struct fpathconf_args *uap; | |
729 | register_t *retval; | |
730 | { | |
731 | int fd = uap->fd; | |
732 | struct filedesc *fdp = p->p_fd; | |
733 | struct file *fp; | |
734 | struct vnode *vp; | |
735 | ||
736 | if ((u_int)fd >= fdp->fd_nfiles || | |
737 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
738 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
739 | return (EBADF); | |
740 | switch (fp->f_type) { | |
741 | ||
742 | case DTYPE_SOCKET: | |
743 | if (uap->name != _PC_PIPE_BUF) | |
744 | return (EINVAL); | |
745 | *retval = PIPE_BUF; | |
746 | return (0); | |
747 | ||
748 | case DTYPE_VNODE: | |
749 | vp = (struct vnode *)fp->f_data; | |
750 | return (VOP_PATHCONF(vp, uap->name, retval)); | |
751 | ||
752 | default: | |
753 | panic("fpathconf"); | |
754 | } | |
755 | /*NOTREACHED*/ | |
756 | } | |
757 | ||
758 | /* | |
759 | * Allocate a file descriptor for the process. | |
760 | */ | |
761 | int fdexpand; | |
762 | ||
763 | int | |
764 | fdalloc(p, want, result) | |
765 | struct proc *p; | |
766 | int want; | |
767 | int *result; | |
768 | { | |
769 | register struct filedesc *fdp = p->p_fd; | |
770 | register int i; | |
771 | int lim, last, nfiles, oldnfiles; | |
772 | struct file **newofiles, **ofiles; | |
773 | char *newofileflags, *ofileflags; | |
774 | ||
775 | /* | |
776 | * Search for a free descriptor starting at the higher | |
777 | * of want or fd_freefile. If that fails, consider | |
778 | * expanding the ofile array. | |
779 | */ | |
780 | lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); | |
781 | for (;;) { | |
782 | last = min(fdp->fd_nfiles, lim); | |
783 | if ((i = want) < fdp->fd_freefile) | |
784 | i = fdp->fd_freefile; | |
785 | ofiles = &fdp->fd_ofiles[i]; | |
786 | ofileflags = &fdp->fd_ofileflags[i]; | |
787 | for (; i < last; i++) { | |
788 | if (*ofiles == NULL && !(*ofileflags & UF_RESERVED)) { | |
789 | *ofileflags = UF_RESERVED; | |
790 | if (i > fdp->fd_lastfile) | |
791 | fdp->fd_lastfile = i; | |
792 | if (want <= fdp->fd_freefile) | |
793 | fdp->fd_freefile = i; | |
794 | *result = i; | |
795 | return (0); | |
796 | } | |
797 | ofiles++; ofileflags++; | |
798 | } | |
799 | ||
800 | /* | |
801 | * No space in current array. Expand? | |
802 | */ | |
803 | if (fdp->fd_nfiles >= lim) | |
804 | return (EMFILE); | |
805 | if (fdp->fd_nfiles < NDEXTENT) | |
806 | nfiles = NDEXTENT; | |
807 | else | |
808 | nfiles = 2 * fdp->fd_nfiles; | |
809 | /* Enforce lim */ | |
810 | if (nfiles > lim) | |
811 | nfiles = lim; | |
812 | MALLOC_ZONE(newofiles, struct file **, | |
813 | nfiles * OFILESIZE, M_OFILETABL, M_WAITOK); | |
814 | if (fdp->fd_nfiles >= nfiles) { | |
815 | FREE_ZONE(newofiles, nfiles * OFILESIZE, M_OFILETABL); | |
816 | continue; | |
817 | } | |
818 | newofileflags = (char *) &newofiles[nfiles]; | |
819 | /* | |
820 | * Copy the existing ofile and ofileflags arrays | |
821 | * and zero the new portion of each array. | |
822 | */ | |
823 | oldnfiles = fdp->fd_nfiles; | |
824 | (void) memcpy(newofiles, fdp->fd_ofiles, | |
825 | oldnfiles * sizeof *fdp->fd_ofiles); | |
826 | (void) memset(&newofiles[oldnfiles], 0, | |
827 | (nfiles - oldnfiles) * sizeof *fdp->fd_ofiles); | |
828 | ||
829 | (void) memcpy(newofileflags, fdp->fd_ofileflags, | |
830 | oldnfiles * sizeof *fdp->fd_ofileflags); | |
831 | (void) memset(&newofileflags[oldnfiles], 0, | |
832 | (nfiles - oldnfiles) * | |
833 | sizeof *fdp->fd_ofileflags); | |
834 | ofiles = fdp->fd_ofiles; | |
835 | fdp->fd_ofiles = newofiles; | |
836 | fdp->fd_ofileflags = newofileflags; | |
837 | fdp->fd_nfiles = nfiles; | |
838 | FREE_ZONE(ofiles, oldnfiles * OFILESIZE, M_OFILETABL); | |
839 | fdexpand++; | |
840 | } | |
841 | } | |
842 | ||
843 | /* | |
844 | * Check to see whether n user file descriptors | |
845 | * are available to the process p. | |
846 | */ | |
847 | int | |
848 | fdavail(p, n) | |
849 | struct proc *p; | |
850 | register int n; | |
851 | { | |
852 | register struct filedesc *fdp = p->p_fd; | |
853 | register struct file **fpp; | |
854 | register char *flags; | |
855 | register int i, lim; | |
856 | ||
857 | lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); | |
858 | if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) | |
859 | return (1); | |
860 | fpp = &fdp->fd_ofiles[fdp->fd_freefile]; | |
861 | flags = &fdp->fd_ofileflags[fdp->fd_freefile]; | |
862 | for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++, flags++) | |
863 | if (*fpp == NULL && !(*flags & UF_RESERVED) && --n <= 0) | |
864 | return (1); | |
865 | return (0); | |
866 | } | |
867 | ||
868 | void | |
869 | fdrelse(p, fd) | |
870 | struct proc *p; | |
871 | int fd; | |
872 | { | |
873 | _fdrelse(p->p_fd, fd); | |
874 | } | |
875 | ||
876 | int | |
877 | fdgetf(p, fd, resultfp) | |
878 | register struct proc *p; | |
879 | register int fd; | |
880 | struct file **resultfp; | |
881 | { | |
882 | register struct filedesc *fdp = p->p_fd; | |
883 | struct file *fp; | |
884 | ||
885 | if ((u_int)fd >= fdp->fd_nfiles || | |
886 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
887 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
888 | return (EBADF); | |
889 | ||
890 | if (resultfp) | |
891 | *resultfp = fp; | |
892 | return (0); | |
893 | } | |
894 | ||
895 | /* | |
896 | * Create a new open file structure and allocate | |
897 | * a file decriptor for the process that refers to it. | |
898 | */ | |
899 | int | |
900 | falloc(p, resultfp, resultfd) | |
901 | register struct proc *p; | |
902 | struct file **resultfp; | |
903 | int *resultfd; | |
904 | { | |
905 | register struct file *fp, *fq; | |
906 | int error, i; | |
907 | ||
908 | if (error = fdalloc(p, 0, &i)) | |
909 | return (error); | |
910 | if (nfiles >= maxfiles) { | |
911 | tablefull("file"); | |
912 | return (ENFILE); | |
913 | } | |
914 | /* | |
915 | * Allocate a new file descriptor. | |
916 | * If the process has file descriptor zero open, add to the list | |
917 | * of open files at that point, otherwise put it at the front of | |
918 | * the list of open files. | |
919 | */ | |
920 | nfiles++; | |
921 | MALLOC_ZONE(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK); | |
922 | bzero(fp, sizeof(struct file)); | |
923 | if (fq = p->p_fd->fd_ofiles[0]) { | |
924 | LIST_INSERT_AFTER(fq, fp, f_list); | |
925 | } else { | |
926 | LIST_INSERT_HEAD(&filehead, fp, f_list); | |
927 | } | |
928 | p->p_fd->fd_ofiles[i] = fp; | |
929 | fp->f_count = 1; | |
930 | fp->f_cred = p->p_ucred; | |
931 | crhold(fp->f_cred); | |
932 | if (resultfp) | |
933 | *resultfp = fp; | |
934 | if (resultfd) | |
935 | *resultfd = i; | |
936 | return (0); | |
937 | } | |
938 | ||
939 | /* | |
940 | * Free a file structure. | |
941 | */ | |
942 | void | |
943 | ffree(fp) | |
944 | register struct file *fp; | |
945 | { | |
946 | register struct file *fq; | |
947 | struct ucred *cred; | |
948 | ||
949 | LIST_REMOVE(fp, f_list); | |
950 | cred = fp->f_cred; | |
951 | if (cred != NOCRED) { | |
952 | fp->f_cred = NOCRED; | |
953 | crfree(cred); | |
954 | } | |
fa4905b1 | 955 | |
1c79356b | 956 | nfiles--; |
d7e50217 A |
957 | memset(fp, 0xff, sizeof *fp); |
958 | fp->f_count = (short)0xffff; | |
959 | ||
1c79356b A |
960 | FREE_ZONE(fp, sizeof *fp, M_FILE); |
961 | } | |
962 | ||
963 | void | |
964 | fdexec(p) | |
965 | struct proc *p; | |
966 | { | |
967 | register struct filedesc *fdp = p->p_fd; | |
968 | register int i = fdp->fd_lastfile; | |
969 | register struct file **fpp = &fdp->fd_ofiles[i]; | |
970 | register char *flags = &fdp->fd_ofileflags[i]; | |
971 | ||
972 | while (i >= 0) { | |
973 | if ((*flags & (UF_RESERVED|UF_EXCLOSE)) == UF_EXCLOSE) { | |
974 | register struct file *fp = *fpp; | |
975 | ||
976 | *fpp = NULL; *flags = 0; | |
977 | if (i == fdp->fd_lastfile && i > 0) | |
978 | fdp->fd_lastfile--; | |
979 | closef(fp, p); | |
980 | } | |
981 | else | |
982 | *flags &= ~UF_MAPPED; | |
983 | ||
984 | i--; fpp--; flags--; | |
985 | } | |
986 | } | |
987 | ||
988 | /* | |
989 | * Copy a filedesc structure. | |
990 | */ | |
991 | struct filedesc * | |
992 | fdcopy(p) | |
993 | struct proc *p; | |
994 | { | |
995 | register struct filedesc *newfdp, *fdp = p->p_fd; | |
996 | register int i; | |
997 | ||
998 | MALLOC_ZONE(newfdp, struct filedesc *, | |
999 | sizeof *newfdp, M_FILEDESC, M_WAITOK); | |
1000 | (void) memcpy(newfdp, fdp, sizeof *newfdp); | |
1001 | VREF(newfdp->fd_cdir); | |
1002 | if (newfdp->fd_rdir) | |
1003 | VREF(newfdp->fd_rdir); | |
1004 | newfdp->fd_refcnt = 1; | |
1005 | ||
1006 | /* | |
1007 | * If the number of open files fits in the internal arrays | |
1008 | * of the open file structure, use them, otherwise allocate | |
1009 | * additional memory for the number of descriptors currently | |
1010 | * in use. | |
1011 | */ | |
1012 | if (newfdp->fd_lastfile < NDFILE) | |
1013 | i = NDFILE; | |
1014 | else { | |
1015 | /* | |
1016 | * Compute the smallest multiple of NDEXTENT needed | |
1017 | * for the file descriptors currently in use, | |
1018 | * allowing the table to shrink. | |
1019 | */ | |
1020 | i = newfdp->fd_nfiles; | |
1021 | while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2) | |
1022 | i /= 2; | |
1023 | } | |
1024 | MALLOC_ZONE(newfdp->fd_ofiles, struct file **, | |
1025 | i * OFILESIZE, M_OFILETABL, M_WAITOK); | |
1026 | newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i]; | |
1027 | newfdp->fd_nfiles = i; | |
1028 | if (fdp->fd_nfiles > 0) { | |
1029 | register struct file **fpp; | |
1030 | register char *flags; | |
1031 | ||
1032 | (void) memcpy(newfdp->fd_ofiles, fdp->fd_ofiles, | |
1033 | i * sizeof *fdp->fd_ofiles); | |
1034 | (void) memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, | |
1035 | i * sizeof *fdp->fd_ofileflags); | |
1036 | ||
1037 | fpp = newfdp->fd_ofiles; | |
1038 | flags = newfdp->fd_ofileflags; | |
1039 | for (i = newfdp->fd_lastfile; i-- >= 0; fpp++, flags++) | |
1040 | if (*fpp != NULL && !(*flags & UF_RESERVED)) { | |
1041 | (void)fref(*fpp); | |
1042 | } else { | |
1043 | *fpp = NULL; | |
1044 | *flags = 0; | |
1045 | } | |
fa4905b1 | 1046 | } else |
1c79356b A |
1047 | (void) memset(newfdp->fd_ofiles, 0, i * OFILESIZE); |
1048 | ||
1049 | return (newfdp); | |
1050 | } | |
1051 | ||
1052 | /* | |
1053 | * Release a filedesc structure. | |
1054 | */ | |
1055 | void | |
1056 | fdfree(p) | |
1057 | struct proc *p; | |
1058 | { | |
fa4905b1 A |
1059 | struct filedesc *fdp; |
1060 | struct file **fpp; | |
1061 | int i; | |
1062 | struct vnode *tvp; | |
1c79356b A |
1063 | |
1064 | if ((fdp = p->p_fd) == NULL) | |
1065 | return; | |
1066 | if (--fdp->fd_refcnt > 0) | |
1067 | return; | |
1068 | p->p_fd = NULL; | |
1069 | if (fdp->fd_nfiles > 0) { | |
1070 | fpp = fdp->fd_ofiles; | |
1071 | for (i = fdp->fd_lastfile; i-- >= 0; fpp++) | |
1072 | if (*fpp) | |
1073 | (void) closef(*fpp, p); | |
1074 | FREE_ZONE(fdp->fd_ofiles, | |
1075 | fdp->fd_nfiles * OFILESIZE, M_OFILETABL); | |
1076 | } | |
fa4905b1 A |
1077 | tvp = fdp->fd_cdir; |
1078 | fdp->fd_cdir = NULL; | |
1079 | vrele(tvp); | |
1080 | if (fdp->fd_rdir) { | |
1081 | tvp = fdp->fd_rdir; | |
1082 | fdp->fd_rdir = NULL; | |
1083 | vrele(tvp); | |
1084 | } | |
1c79356b A |
1085 | FREE_ZONE(fdp, sizeof *fdp, M_FILEDESC); |
1086 | } | |
1087 | ||
9bccf70c A |
1088 | static int |
1089 | closef_finish(fp, p) | |
1090 | register struct file *fp; | |
1091 | register struct proc *p; | |
1092 | { | |
1093 | struct vnode *vp; | |
1094 | struct flock lf; | |
1095 | int error; | |
1096 | ||
1097 | if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) { | |
1098 | lf.l_whence = SEEK_SET; | |
1099 | lf.l_start = 0; | |
1100 | lf.l_len = 0; | |
1101 | lf.l_type = F_UNLCK; | |
1102 | vp = (struct vnode *)fp->f_data; | |
1103 | (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); | |
1104 | } | |
1105 | if (fp->f_ops) | |
1106 | error = fo_close(fp, p); | |
1107 | else | |
1108 | error = 0; | |
1109 | ffree(fp); | |
1110 | return (error); | |
1111 | } | |
1112 | ||
1c79356b A |
1113 | /* |
1114 | * Internal form of close. | |
1115 | * Decrement reference count on file structure. | |
1116 | * Note: p may be NULL when closing a file | |
1117 | * that was being passed in a message. | |
1118 | */ | |
1119 | int | |
1120 | closef(fp, p) | |
1121 | register struct file *fp; | |
1122 | register struct proc *p; | |
1123 | { | |
1124 | struct vnode *vp; | |
1125 | struct flock lf; | |
1126 | int error; | |
1127 | ||
1128 | if (fp == NULL) | |
1129 | return (0); | |
1130 | /* | |
1131 | * POSIX record locking dictates that any close releases ALL | |
1132 | * locks owned by this process. This is handled by setting | |
1133 | * a flag in the unlock to free ONLY locks obeying POSIX | |
1134 | * semantics, and not to free BSD-style file locks. | |
1135 | * If the descriptor was in a message, POSIX-style locks | |
1136 | * aren't passed with the descriptor. | |
1137 | */ | |
1138 | if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) { | |
1139 | lf.l_whence = SEEK_SET; | |
1140 | lf.l_start = 0; | |
1141 | lf.l_len = 0; | |
1142 | lf.l_type = F_UNLCK; | |
1143 | vp = (struct vnode *)fp->f_data; | |
1144 | (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX); | |
1145 | } | |
9bccf70c | 1146 | if (frele_internal(fp) > 0) |
1c79356b | 1147 | return (0); |
9bccf70c | 1148 | return(closef_finish(fp, p)); |
1c79356b A |
1149 | } |
1150 | ||
1151 | /* | |
1152 | * Apply an advisory lock on a file descriptor. | |
1153 | * | |
1154 | * Just attempt to get a record lock of the requested type on | |
1155 | * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0). | |
1156 | */ | |
1157 | struct flock_args { | |
1158 | int fd; | |
1159 | int how; | |
1160 | }; | |
1161 | /* ARGSUSED */ | |
1162 | int | |
1163 | flock(p, uap, retval) | |
1164 | struct proc *p; | |
1165 | register struct flock_args *uap; | |
1166 | register_t *retval; | |
1167 | { | |
1168 | int fd = uap->fd; | |
1169 | int how = uap->how; | |
1170 | register struct filedesc *fdp = p->p_fd; | |
1171 | register struct file *fp; | |
1172 | struct vnode *vp; | |
1173 | struct flock lf; | |
1174 | ||
1175 | if ((u_int)fd >= fdp->fd_nfiles || | |
1176 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
1177 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) | |
1178 | return (EBADF); | |
1179 | if (fp->f_type != DTYPE_VNODE) | |
1180 | return (EOPNOTSUPP); | |
1181 | vp = (struct vnode *)fp->f_data; | |
1182 | lf.l_whence = SEEK_SET; | |
1183 | lf.l_start = 0; | |
1184 | lf.l_len = 0; | |
1185 | if (how & LOCK_UN) { | |
1186 | lf.l_type = F_UNLCK; | |
1187 | fp->f_flag &= ~FHASLOCK; | |
1188 | return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK)); | |
1189 | } | |
1190 | if (how & LOCK_EX) | |
1191 | lf.l_type = F_WRLCK; | |
1192 | else if (how & LOCK_SH) | |
1193 | lf.l_type = F_RDLCK; | |
1194 | else | |
1195 | return (EBADF); | |
1196 | fp->f_flag |= FHASLOCK; | |
1197 | if (how & LOCK_NB) | |
1198 | return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK)); | |
1199 | return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT)); | |
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * File Descriptor pseudo-device driver (/dev/fd/). | |
1204 | * | |
1205 | * Opening minor device N dup()s the file (if any) connected to file | |
1206 | * descriptor N belonging to the calling process. Note that this driver | |
1207 | * consists of only the ``open()'' routine, because all subsequent | |
1208 | * references to this file will be direct to the other driver. | |
1209 | */ | |
1210 | /* ARGSUSED */ | |
1211 | int | |
1212 | fdopen(dev, mode, type, p) | |
1213 | dev_t dev; | |
1214 | int mode, type; | |
1215 | struct proc *p; | |
1216 | { | |
1217 | ||
1218 | /* | |
1219 | * XXX Kludge: set curproc->p_dupfd to contain the value of the | |
1220 | * the file descriptor being sought for duplication. The error | |
1221 | * return ensures that the vnode for this device will be released | |
1222 | * by vn_open. Open will detect this special error and take the | |
1223 | * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN | |
1224 | * will simply report the error. | |
1225 | */ | |
1226 | p->p_dupfd = minor(dev); | |
1227 | return (ENODEV); | |
1228 | } | |
1229 | ||
1230 | /* | |
1231 | * Duplicate the specified descriptor to a free descriptor. | |
1232 | */ | |
1233 | int | |
1234 | dupfdopen(fdp, indx, dfd, mode, error) | |
1235 | register struct filedesc *fdp; | |
1236 | register int indx, dfd; | |
1237 | int mode; | |
1238 | int error; | |
1239 | { | |
1240 | register struct file *wfp; | |
1241 | struct file *fp; | |
1242 | ||
1243 | /* | |
1244 | * If the to-be-dup'd fd number is greater than the allowed number | |
1245 | * of file descriptors, or the fd to be dup'd has already been | |
1246 | * closed, reject. Note, check for new == old is necessary as | |
1247 | * falloc could allocate an already closed to-be-dup'd descriptor | |
1248 | * as the new descriptor. | |
1249 | */ | |
1250 | fp = fdp->fd_ofiles[indx]; | |
1251 | if ((u_int)dfd >= fdp->fd_nfiles || | |
1252 | (wfp = fdp->fd_ofiles[dfd]) == NULL || wfp == fp || | |
1253 | (fdp->fd_ofileflags[dfd] & UF_RESERVED)) | |
1254 | return (EBADF); | |
1255 | ||
1256 | /* | |
1257 | * There are two cases of interest here. | |
1258 | * | |
1259 | * For ENODEV simply dup (dfd) to file descriptor | |
1260 | * (indx) and return. | |
1261 | * | |
1262 | * For ENXIO steal away the file structure from (dfd) and | |
1263 | * store it in (indx). (dfd) is effectively closed by | |
1264 | * this operation. | |
1265 | * | |
1266 | * Any other error code is just returned. | |
1267 | */ | |
1268 | switch (error) { | |
1269 | case ENODEV: | |
1270 | /* | |
1271 | * Check that the mode the file is being opened for is a | |
1272 | * subset of the mode of the existing descriptor. | |
1273 | */ | |
1274 | if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) | |
1275 | return (EACCES); | |
1276 | (void)fref(wfp); | |
1277 | if (indx > fdp->fd_lastfile) | |
1278 | fdp->fd_lastfile = indx;; | |
1279 | fdp->fd_ofiles[indx] = wfp; | |
1280 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; | |
1281 | return (0); | |
1282 | ||
1283 | case ENXIO: | |
1284 | /* | |
1285 | * Steal away the file pointer from dfd, and stuff it into indx. | |
1286 | */ | |
1287 | if (indx > fdp->fd_lastfile) | |
1288 | fdp->fd_lastfile = indx;; | |
1289 | fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd]; | |
1290 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; | |
1291 | _fdrelse(fdp, dfd); | |
1292 | return (0); | |
1293 | ||
1294 | default: | |
1295 | return (error); | |
1296 | } | |
1297 | /* NOTREACHED */ | |
1298 | } | |
1299 | ||
1300 | /* Reference manipulation routines for the file structure */ | |
1301 | ||
1302 | int | |
1303 | fref(struct file *fp) | |
1304 | { | |
d7e50217 A |
1305 | if (fp->f_count == (short)0xffff) |
1306 | return (-1); | |
1c79356b A |
1307 | if (++fp->f_count <= 0) |
1308 | panic("fref: f_count"); | |
1309 | return ((int)fp->f_count); | |
1310 | } | |
1311 | ||
9bccf70c A |
1312 | static int |
1313 | frele_internal(struct file *fp) | |
1c79356b | 1314 | { |
d7e50217 A |
1315 | if (fp->f_count == (short)0xffff) |
1316 | panic("frele: stale"); | |
1c79356b A |
1317 | if (--fp->f_count < 0) |
1318 | panic("frele: count < 0"); | |
1319 | return ((int)fp->f_count); | |
1320 | } | |
1321 | ||
9bccf70c A |
1322 | |
1323 | int | |
1324 | frele(struct file *fp) | |
1325 | { | |
1326 | int count; | |
1327 | funnel_t * fnl; | |
1328 | extern int disable_funnel; | |
1329 | ||
1330 | fnl = thread_funnel_get(); | |
1331 | /* | |
1332 | * If the funnels are merged then atleast a funnel should be held | |
1333 | * else frele should come in with kernel funnel only | |
1334 | */ | |
1335 | if (!disable_funnel && (fnl != kernel_flock)) { | |
1336 | panic("frele: kernel funnel not held"); | |
1337 | ||
1338 | } else if (fnl == THR_FUNNEL_NULL) { | |
1339 | panic("frele: no funnel held"); | |
1340 | } | |
1341 | ||
1342 | if ((count = frele_internal(fp)) == 0) { | |
1343 | /* some one closed the fd while we were blocked */ | |
1344 | (void)closef_finish(fp, current_proc()); | |
1345 | } | |
1346 | return(count); | |
1347 | } | |
1348 | ||
1c79356b A |
1349 | int |
1350 | fcount(struct file *fp) | |
1351 | { | |
d7e50217 A |
1352 | if (fp->f_count == (short)0xffff) |
1353 | panic("fcount: stale"); | |
1c79356b A |
1354 | return ((int)fp->f_count); |
1355 | } | |
1356 |