Deleted Added
full compact
kern_descrip.c (82516) kern_descrip.c (82749)
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 22 unchanged lines hidden (view full) ---

31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 22 unchanged lines hidden (view full) ---

31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
39 * $FreeBSD: head/sys/kern/kern_descrip.c 82516 2001-08-29 18:53:53Z ache $
39 * $FreeBSD: head/sys/kern/kern_descrip.c 82749 2001-09-01 19:04:37Z dillon $
40 */
41
42#include "opt_compat.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>

--- 63 unchanged lines hidden (view full) ---

111/*
112 * System calls on descriptors.
113 */
114#ifndef _SYS_SYSPROTO_H_
115struct getdtablesize_args {
116 int dummy;
117};
118#endif
40 */
41
42#include "opt_compat.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>

--- 63 unchanged lines hidden (view full) ---

111/*
112 * System calls on descriptors.
113 */
114#ifndef _SYS_SYSPROTO_H_
115struct getdtablesize_args {
116 int dummy;
117};
118#endif
119/*
120 * MPSAFE
121 */
119/* ARGSUSED */
120int
121getdtablesize(p, uap)
122 struct proc *p;
123 struct getdtablesize_args *uap;
124{
125
122/* ARGSUSED */
123int
124getdtablesize(p, uap)
125 struct proc *p;
126 struct getdtablesize_args *uap;
127{
128
129 mtx_lock(&Giant);
126 p->p_retval[0] =
127 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
130 p->p_retval[0] =
131 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
132 mtx_unlock(&Giant);
128 return (0);
129}
130
131/*
132 * Duplicate a file descriptor to a particular value.
133 *
134 * note: keep in mind that a potential race condition exists when closing
135 * descriptors from a shared descriptor table (via rfork).
136 */
137#ifndef _SYS_SYSPROTO_H_
138struct dup2_args {
139 u_int from;
140 u_int to;
141};
142#endif
133 return (0);
134}
135
136/*
137 * Duplicate a file descriptor to a particular value.
138 *
139 * note: keep in mind that a potential race condition exists when closing
140 * descriptors from a shared descriptor table (via rfork).
141 */
142#ifndef _SYS_SYSPROTO_H_
143struct dup2_args {
144 u_int from;
145 u_int to;
146};
147#endif
148/*
149 * MPSAFE
150 */
143/* ARGSUSED */
144int
145dup2(p, uap)
146 struct proc *p;
147 struct dup2_args *uap;
148{
149 register struct filedesc *fdp = p->p_fd;
150 register u_int old = uap->from, new = uap->to;
151 int i, error;
152
151/* ARGSUSED */
152int
153dup2(p, uap)
154 struct proc *p;
155 struct dup2_args *uap;
156{
157 register struct filedesc *fdp = p->p_fd;
158 register u_int old = uap->from, new = uap->to;
159 int i, error;
160
161 mtx_lock(&Giant);
153retry:
154 if (old >= fdp->fd_nfiles ||
155 fdp->fd_ofiles[old] == NULL ||
156 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
157 new >= maxfilesperproc) {
162retry:
163 if (old >= fdp->fd_nfiles ||
164 fdp->fd_ofiles[old] == NULL ||
165 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
166 new >= maxfilesperproc) {
158 return (EBADF);
167 error = EBADF;
168 goto done2;
159 }
160 if (old == new) {
161 p->p_retval[0] = new;
169 }
170 if (old == new) {
171 p->p_retval[0] = new;
162 return (0);
172 error = 0;
173 goto done2;
163 }
164 if (new >= fdp->fd_nfiles) {
165 if ((error = fdalloc(p, new, &i)))
174 }
175 if (new >= fdp->fd_nfiles) {
176 if ((error = fdalloc(p, new, &i)))
166 return (error);
177 goto done2;
167 if (new != i)
168 panic("dup2: fdalloc");
169 /*
170 * fdalloc() may block, retest everything.
171 */
172 goto retry;
173 }
178 if (new != i)
179 panic("dup2: fdalloc");
180 /*
181 * fdalloc() may block, retest everything.
182 */
183 goto retry;
184 }
174 return (do_dup(fdp, (int)old, (int)new, p->p_retval, p));
185 error = do_dup(fdp, (int)old, (int)new, p->p_retval, p);
186done2:
187 mtx_unlock(&Giant);
188 return(error);
175}
176
177/*
178 * Duplicate a file descriptor.
179 */
180#ifndef _SYS_SYSPROTO_H_
181struct dup_args {
182 u_int fd;
183};
184#endif
189}
190
191/*
192 * Duplicate a file descriptor.
193 */
194#ifndef _SYS_SYSPROTO_H_
195struct dup_args {
196 u_int fd;
197};
198#endif
199/*
200 * MPSAFE
201 */
185/* ARGSUSED */
186int
187dup(p, uap)
188 struct proc *p;
189 struct dup_args *uap;
190{
191 register struct filedesc *fdp;
192 u_int old;
193 int new, error;
194
202/* ARGSUSED */
203int
204dup(p, uap)
205 struct proc *p;
206 struct dup_args *uap;
207{
208 register struct filedesc *fdp;
209 u_int old;
210 int new, error;
211
212 mtx_lock(&Giant);
195 old = uap->fd;
196 fdp = p->p_fd;
213 old = uap->fd;
214 fdp = p->p_fd;
197 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
198 return (EBADF);
215 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
216 error = EBADF;
217 goto done2;
218 }
199 if ((error = fdalloc(p, 0, &new)))
219 if ((error = fdalloc(p, 0, &new)))
200 return (error);
201 return (do_dup(fdp, (int)old, new, p->p_retval, p));
220 goto done2;
221 error = do_dup(fdp, (int)old, new, p->p_retval, p);
222done2:
223 mtx_unlock(&Giant);
224 return (error);
202}
203
204/*
205 * The file control system call.
206 */
207#ifndef _SYS_SYSPROTO_H_
208struct fcntl_args {
209 int fd;
210 int cmd;
211 long arg;
212};
213#endif
225}
226
227/*
228 * The file control system call.
229 */
230#ifndef _SYS_SYSPROTO_H_
231struct fcntl_args {
232 int fd;
233 int cmd;
234 long arg;
235};
236#endif
237/*
238 * MPSAFE
239 */
214/* ARGSUSED */
215int
216fcntl(p, uap)
217 struct proc *p;
218 register struct fcntl_args *uap;
219{
240/* ARGSUSED */
241int
242fcntl(p, uap)
243 struct proc *p;
244 register struct fcntl_args *uap;
245{
220 register struct filedesc *fdp = p->p_fd;
246 register struct filedesc *fdp;
221 register struct file *fp;
222 register char *pop;
223 struct vnode *vp;
247 register struct file *fp;
248 register char *pop;
249 struct vnode *vp;
224 int i, tmp, error, flg = F_POSIX;
250 int i, tmp, error = 0, flg = F_POSIX;
225 struct flock fl;
226 u_int newmin;
227
251 struct flock fl;
252 u_int newmin;
253
254 mtx_lock(&Giant);
255
256 fdp = p->p_fd;
228 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
257 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
229 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
230 return (EBADF);
258 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
259 error = EBADF;
260 goto done2;
261 }
231 pop = &fdp->fd_ofileflags[uap->fd];
232
233 switch (uap->cmd) {
234 case F_DUPFD:
235 newmin = uap->arg;
236 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
262 pop = &fdp->fd_ofileflags[uap->fd];
263
264 switch (uap->cmd) {
265 case F_DUPFD:
266 newmin = uap->arg;
267 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
237 newmin >= maxfilesperproc)
238 return (EINVAL);
268 newmin >= maxfilesperproc) {
269 error = EINVAL;
270 break;
271 }
239 if ((error = fdalloc(p, newmin, &i)))
272 if ((error = fdalloc(p, newmin, &i)))
240 return (error);
241 return (do_dup(fdp, uap->fd, i, p->p_retval, p));
273 break;
274 error = do_dup(fdp, uap->fd, i, p->p_retval, p);
275 break;
242
243 case F_GETFD:
244 p->p_retval[0] = *pop & 1;
276
277 case F_GETFD:
278 p->p_retval[0] = *pop & 1;
245 return (0);
279 break;
246
247 case F_SETFD:
248 *pop = (*pop &~ 1) | (uap->arg & 1);
280
281 case F_SETFD:
282 *pop = (*pop &~ 1) | (uap->arg & 1);
249 return (0);
283 break;
250
251 case F_GETFL:
252 p->p_retval[0] = OFLAGS(fp->f_flag);
284
285 case F_GETFL:
286 p->p_retval[0] = OFLAGS(fp->f_flag);
253 return (0);
287 break;
254
255 case F_SETFL:
256 fhold(fp);
257 fp->f_flag &= ~FCNTLFLAGS;
258 fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
259 tmp = fp->f_flag & FNONBLOCK;
260 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
261 if (error) {
262 fdrop(fp, p);
288
289 case F_SETFL:
290 fhold(fp);
291 fp->f_flag &= ~FCNTLFLAGS;
292 fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
293 tmp = fp->f_flag & FNONBLOCK;
294 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
295 if (error) {
296 fdrop(fp, p);
263 return (error);
297 break;
264 }
265 tmp = fp->f_flag & FASYNC;
266 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
267 if (!error) {
268 fdrop(fp, p);
298 }
299 tmp = fp->f_flag & FASYNC;
300 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
301 if (!error) {
302 fdrop(fp, p);
269 return (0);
303 break;
270 }
271 fp->f_flag &= ~FNONBLOCK;
272 tmp = 0;
273 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
274 fdrop(fp, p);
304 }
305 fp->f_flag &= ~FNONBLOCK;
306 tmp = 0;
307 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
308 fdrop(fp, p);
275 return (error);
309 break;
276
277 case F_GETOWN:
278 fhold(fp);
279 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)p->p_retval, p);
280 fdrop(fp, p);
310
311 case F_GETOWN:
312 fhold(fp);
313 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)p->p_retval, p);
314 fdrop(fp, p);
281 return(error);
315 break;
282
283 case F_SETOWN:
284 fhold(fp);
285 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, p);
286 fdrop(fp, p);
316
317 case F_SETOWN:
318 fhold(fp);
319 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, p);
320 fdrop(fp, p);
287 return(error);
321 break;
288
289 case F_SETLKW:
290 flg |= F_WAIT;
291 /* Fall into F_SETLK */
292
293 case F_SETLK:
322
323 case F_SETLKW:
324 flg |= F_WAIT;
325 /* Fall into F_SETLK */
326
327 case F_SETLK:
294 if (fp->f_type != DTYPE_VNODE)
295 return (EBADF);
328 if (fp->f_type != DTYPE_VNODE) {
329 error = EBADF;
330 break;
331 }
296 vp = (struct vnode *)fp->f_data;
297
298 /*
299 * copyin/lockop may block
300 */
301 fhold(fp);
302 /* Copy in the lock structure */
303 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
304 sizeof(fl));
305 if (error) {
306 fdrop(fp, p);
332 vp = (struct vnode *)fp->f_data;
333
334 /*
335 * copyin/lockop may block
336 */
337 fhold(fp);
338 /* Copy in the lock structure */
339 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
340 sizeof(fl));
341 if (error) {
342 fdrop(fp, p);
307 return (error);
343 break;
308 }
309 if (fl.l_whence == SEEK_CUR) {
310 if (fp->f_offset < 0 ||
311 (fl.l_start > 0 &&
312 fp->f_offset > OFF_MAX - fl.l_start)) {
313 fdrop(fp, p);
344 }
345 if (fl.l_whence == SEEK_CUR) {
346 if (fp->f_offset < 0 ||
347 (fl.l_start > 0 &&
348 fp->f_offset > OFF_MAX - fl.l_start)) {
349 fdrop(fp, p);
314 return (EOVERFLOW);
350 error = EOVERFLOW;
351 break;
315 }
316 fl.l_start += fp->f_offset;
317 }
318
319 switch (fl.l_type) {
320 case F_RDLCK:
321 if ((fp->f_flag & FREAD) == 0) {
322 error = EBADF;

--- 16 unchanged lines hidden (view full) ---

339 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
340 &fl, F_POSIX);
341 break;
342 default:
343 error = EINVAL;
344 break;
345 }
346 fdrop(fp, p);
352 }
353 fl.l_start += fp->f_offset;
354 }
355
356 switch (fl.l_type) {
357 case F_RDLCK:
358 if ((fp->f_flag & FREAD) == 0) {
359 error = EBADF;

--- 16 unchanged lines hidden (view full) ---

376 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
377 &fl, F_POSIX);
378 break;
379 default:
380 error = EINVAL;
381 break;
382 }
383 fdrop(fp, p);
347 return(error);
348
384 break;
349 case F_GETLK:
385 case F_GETLK:
350 if (fp->f_type != DTYPE_VNODE)
351 return (EBADF);
386 if (fp->f_type != DTYPE_VNODE) {
387 error = EBADF;
388 break;
389 }
352 vp = (struct vnode *)fp->f_data;
353 /*
354 * copyin/lockop may block
355 */
356 fhold(fp);
357 /* Copy in the lock structure */
358 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
359 sizeof(fl));
360 if (error) {
361 fdrop(fp, p);
390 vp = (struct vnode *)fp->f_data;
391 /*
392 * copyin/lockop may block
393 */
394 fhold(fp);
395 /* Copy in the lock structure */
396 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
397 sizeof(fl));
398 if (error) {
399 fdrop(fp, p);
362 return (error);
400 break;
363 }
364 if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK &&
365 fl.l_type != F_UNLCK) {
366 fdrop(fp, p);
401 }
402 if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK &&
403 fl.l_type != F_UNLCK) {
404 fdrop(fp, p);
367 return (EINVAL);
405 error = EINVAL;
406 break;
368 }
369 if (fl.l_whence == SEEK_CUR) {
370 if ((fl.l_start > 0 &&
371 fp->f_offset > OFF_MAX - fl.l_start) ||
372 (fl.l_start < 0 &&
373 fp->f_offset < OFF_MIN - fl.l_start)) {
374 fdrop(fp, p);
407 }
408 if (fl.l_whence == SEEK_CUR) {
409 if ((fl.l_start > 0 &&
410 fp->f_offset > OFF_MAX - fl.l_start) ||
411 (fl.l_start < 0 &&
412 fp->f_offset < OFF_MIN - fl.l_start)) {
413 fdrop(fp, p);
375 return (EOVERFLOW);
414 error = EOVERFLOW;
415 break;
376 }
377 fl.l_start += fp->f_offset;
378 }
379 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
380 &fl, F_POSIX);
381 fdrop(fp, p);
382 if (error == 0) {
383 error = copyout((caddr_t)&fl,
384 (caddr_t)(intptr_t)uap->arg, sizeof(fl));
385 }
416 }
417 fl.l_start += fp->f_offset;
418 }
419 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
420 &fl, F_POSIX);
421 fdrop(fp, p);
422 if (error == 0) {
423 error = copyout((caddr_t)&fl,
424 (caddr_t)(intptr_t)uap->arg, sizeof(fl));
425 }
386 return(error);
426 break;
387 default:
427 default:
388 return (EINVAL);
428 error = EINVAL;
429 break;
389 }
430 }
390 /* NOTREACHED */
431done2:
432 mtx_unlock(&Giant);
433 return (error);
391}
392
393/*
394 * Common code for dup, dup2, and fcntl(F_DUPFD).
395 */
396static int
397do_dup(fdp, old, new, retval, p)
398 register struct filedesc *fdp;

--- 164 unchanged lines hidden (view full) ---

563/*
564 * Close a file descriptor.
565 */
566#ifndef _SYS_SYSPROTO_H_
567struct close_args {
568 int fd;
569};
570#endif
434}
435
436/*
437 * Common code for dup, dup2, and fcntl(F_DUPFD).
438 */
439static int
440do_dup(fdp, old, new, retval, p)
441 register struct filedesc *fdp;

--- 164 unchanged lines hidden (view full) ---

606/*
607 * Close a file descriptor.
608 */
609#ifndef _SYS_SYSPROTO_H_
610struct close_args {
611 int fd;
612};
613#endif
614/*
615 * MPSAFE
616 */
571/* ARGSUSED */
572int
573close(p, uap)
574 struct proc *p;
575 struct close_args *uap;
576{
617/* ARGSUSED */
618int
619close(p, uap)
620 struct proc *p;
621 struct close_args *uap;
622{
577 register struct filedesc *fdp = p->p_fd;
623 register struct filedesc *fdp;
578 register struct file *fp;
579 register int fd = uap->fd;
624 register struct file *fp;
625 register int fd = uap->fd;
626 int error = 0;
580
627
628 mtx_lock(&Giant);
629 fdp = p->p_fd;
581 if ((unsigned)fd >= fdp->fd_nfiles ||
630 if ((unsigned)fd >= fdp->fd_nfiles ||
582 (fp = fdp->fd_ofiles[fd]) == NULL)
583 return (EBADF);
631 (fp = fdp->fd_ofiles[fd]) == NULL) {
632 error = EBADF;
633 goto done2;
634 }
584#if 0
585 if (fdp->fd_ofileflags[fd] & UF_MAPPED)
586 (void) munmapfd(p, fd);
587#endif
588 fdp->fd_ofiles[fd] = NULL;
589 fdp->fd_ofileflags[fd] = 0;
590
591 /*
592 * we now hold the fp reference that used to be owned by the descriptor
593 * array.
594 */
595 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
596 fdp->fd_lastfile--;
597 if (fd < fdp->fd_freefile)
598 fdp->fd_freefile = fd;
599 if (fd < fdp->fd_knlistsize)
600 knote_fdclose(p, fd);
635#if 0
636 if (fdp->fd_ofileflags[fd] & UF_MAPPED)
637 (void) munmapfd(p, fd);
638#endif
639 fdp->fd_ofiles[fd] = NULL;
640 fdp->fd_ofileflags[fd] = 0;
641
642 /*
643 * we now hold the fp reference that used to be owned by the descriptor
644 * array.
645 */
646 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
647 fdp->fd_lastfile--;
648 if (fd < fdp->fd_freefile)
649 fdp->fd_freefile = fd;
650 if (fd < fdp->fd_knlistsize)
651 knote_fdclose(p, fd);
601 return (closef(fp, p));
652 error = closef(fp, p);
653done2:
654 mtx_unlock(&Giant);
655 return(error);
602}
603
604#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
605/*
606 * Return status information about a file descriptor.
607 */
608#ifndef _SYS_SYSPROTO_H_
609struct ofstat_args {
610 int fd;
611 struct ostat *sb;
612};
613#endif
656}
657
658#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
659/*
660 * Return status information about a file descriptor.
661 */
662#ifndef _SYS_SYSPROTO_H_
663struct ofstat_args {
664 int fd;
665 struct ostat *sb;
666};
667#endif
668/*
669 * MPSAFE
670 */
614/* ARGSUSED */
615int
616ofstat(p, uap)
617 struct proc *p;
618 register struct ofstat_args *uap;
619{
620 register struct filedesc *fdp = p->p_fd;
621 register struct file *fp;
622 struct stat ub;
623 struct ostat oub;
624 int error;
625
671/* ARGSUSED */
672int
673ofstat(p, uap)
674 struct proc *p;
675 register struct ofstat_args *uap;
676{
677 register struct filedesc *fdp = p->p_fd;
678 register struct file *fp;
679 struct stat ub;
680 struct ostat oub;
681 int error;
682
683 mtx_lock(&Giant);
684
626 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
685 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
627 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
628 return (EBADF);
686 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
687 error = EBADF;
688 goto done2;
689 }
629 fhold(fp);
630 error = fo_stat(fp, &ub, p);
631 if (error == 0) {
632 cvtstat(&ub, &oub);
633 error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
634 }
635 fdrop(fp, p);
690 fhold(fp);
691 error = fo_stat(fp, &ub, p);
692 if (error == 0) {
693 cvtstat(&ub, &oub);
694 error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
695 }
696 fdrop(fp, p);
697done2:
698 mtx_unlock(&Giant);
636 return (error);
637}
638#endif /* COMPAT_43 || COMPAT_SUNOS */
639
640/*
641 * Return status information about a file descriptor.
642 */
643#ifndef _SYS_SYSPROTO_H_
644struct fstat_args {
645 int fd;
646 struct stat *sb;
647};
648#endif
699 return (error);
700}
701#endif /* COMPAT_43 || COMPAT_SUNOS */
702
703/*
704 * Return status information about a file descriptor.
705 */
706#ifndef _SYS_SYSPROTO_H_
707struct fstat_args {
708 int fd;
709 struct stat *sb;
710};
711#endif
712/*
713 * MPSAFE
714 */
649/* ARGSUSED */
650int
651fstat(p, uap)
652 struct proc *p;
653 register struct fstat_args *uap;
654{
715/* ARGSUSED */
716int
717fstat(p, uap)
718 struct proc *p;
719 register struct fstat_args *uap;
720{
655 register struct filedesc *fdp = p->p_fd;
721 register struct filedesc *fdp;
656 register struct file *fp;
657 struct stat ub;
658 int error;
659
722 register struct file *fp;
723 struct stat ub;
724 int error;
725
726 mtx_lock(&Giant);
727 fdp = p->p_fd;
728
660 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
729 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
661 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
662 return (EBADF);
730 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
731 error = EBADF;
732 goto done2;
733 }
663 fhold(fp);
664 error = fo_stat(fp, &ub, p);
665 if (error == 0)
666 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
667 fdrop(fp, p);
734 fhold(fp);
735 error = fo_stat(fp, &ub, p);
736 if (error == 0)
737 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
738 fdrop(fp, p);
739done2:
740 mtx_unlock(&Giant);
668 return (error);
669}
670
671/*
672 * Return status information about a file descriptor.
673 */
674#ifndef _SYS_SYSPROTO_H_
675struct nfstat_args {
676 int fd;
677 struct nstat *sb;
678};
679#endif
741 return (error);
742}
743
744/*
745 * Return status information about a file descriptor.
746 */
747#ifndef _SYS_SYSPROTO_H_
748struct nfstat_args {
749 int fd;
750 struct nstat *sb;
751};
752#endif
753/*
754 * MPSAFE
755 */
680/* ARGSUSED */
681int
682nfstat(p, uap)
683 struct proc *p;
684 register struct nfstat_args *uap;
685{
756/* ARGSUSED */
757int
758nfstat(p, uap)
759 struct proc *p;
760 register struct nfstat_args *uap;
761{
686 register struct filedesc *fdp = p->p_fd;
762 register struct filedesc *fdp;
687 register struct file *fp;
688 struct stat ub;
689 struct nstat nub;
690 int error;
691
763 register struct file *fp;
764 struct stat ub;
765 struct nstat nub;
766 int error;
767
768 mtx_lock(&Giant);
769
770 fdp = p->p_fd;
692 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
771 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
693 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
694 return (EBADF);
772 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
773 error = EBADF;
774 goto done2;
775 }
695 fhold(fp);
696 error = fo_stat(fp, &ub, p);
697 if (error == 0) {
698 cvtnstat(&ub, &nub);
699 error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
700 }
701 fdrop(fp, p);
776 fhold(fp);
777 error = fo_stat(fp, &ub, p);
778 if (error == 0) {
779 cvtnstat(&ub, &nub);
780 error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
781 }
782 fdrop(fp, p);
783done2:
784 mtx_unlock(&Giant);
702 return (error);
703}
704
705/*
706 * Return pathconf information about a file descriptor.
707 */
708#ifndef _SYS_SYSPROTO_H_
709struct fpathconf_args {
710 int fd;
711 int name;
712};
713#endif
785 return (error);
786}
787
788/*
789 * Return pathconf information about a file descriptor.
790 */
791#ifndef _SYS_SYSPROTO_H_
792struct fpathconf_args {
793 int fd;
794 int name;
795};
796#endif
797/*
798 * MPSAFE
799 */
714/* ARGSUSED */
715int
716fpathconf(p, uap)
717 struct proc *p;
718 register struct fpathconf_args *uap;
719{
800/* ARGSUSED */
801int
802fpathconf(p, uap)
803 struct proc *p;
804 register struct fpathconf_args *uap;
805{
720 struct filedesc *fdp = p->p_fd;
806 struct filedesc *fdp;
721 struct file *fp;
722 struct vnode *vp;
723 int error = 0;
724
807 struct file *fp;
808 struct vnode *vp;
809 int error = 0;
810
811 mtx_lock(&Giant);
812 fdp = p->p_fd;
813
725 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
814 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
726 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
727 return (EBADF);
815 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
816 error = EBADF;
817 goto done2;
818 }
728
729 fhold(fp);
730
731 switch (fp->f_type) {
732 case DTYPE_PIPE:
733 case DTYPE_SOCKET:
819
820 fhold(fp);
821
822 switch (fp->f_type) {
823 case DTYPE_PIPE:
824 case DTYPE_SOCKET:
734 if (uap->name != _PC_PIPE_BUF)
735 return (EINVAL);
825 if (uap->name != _PC_PIPE_BUF) {
826 error = EINVAL;
827 goto done2;
828 }
736 p->p_retval[0] = PIPE_BUF;
737 error = 0;
738 break;
739 case DTYPE_FIFO:
740 case DTYPE_VNODE:
741 vp = (struct vnode *)fp->f_data;
742 error = VOP_PATHCONF(vp, uap->name, p->p_retval);
743 break;
744 default:
745 error = EOPNOTSUPP;
746 break;
747 }
748 fdrop(fp, p);
829 p->p_retval[0] = PIPE_BUF;
830 error = 0;
831 break;
832 case DTYPE_FIFO:
833 case DTYPE_VNODE:
834 vp = (struct vnode *)fp->f_data;
835 error = VOP_PATHCONF(vp, uap->name, p->p_retval);
836 break;
837 default:
838 error = EOPNOTSUPP;
839 break;
840 }
841 fdrop(fp, p);
842done2:
843 mtx_unlock(&Giant);
749 return(error);
750}
751
752/*
753 * Allocate a file descriptor for the process.
754 */
755static int fdexpand;
756SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");

--- 512 unchanged lines hidden (view full) ---

1269 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1270 */
1271#ifndef _SYS_SYSPROTO_H_
1272struct flock_args {
1273 int fd;
1274 int how;
1275};
1276#endif
844 return(error);
845}
846
847/*
848 * Allocate a file descriptor for the process.
849 */
850static int fdexpand;
851SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");

--- 512 unchanged lines hidden (view full) ---

1364 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1365 */
1366#ifndef _SYS_SYSPROTO_H_
1367struct flock_args {
1368 int fd;
1369 int how;
1370};
1371#endif
1372/*
1373 * MPSAFE
1374 */
1277/* ARGSUSED */
1278int
1279flock(p, uap)
1280 struct proc *p;
1281 register struct flock_args *uap;
1282{
1283 register struct filedesc *fdp = p->p_fd;
1284 register struct file *fp;
1285 struct vnode *vp;
1286 struct flock lf;
1375/* ARGSUSED */
1376int
1377flock(p, uap)
1378 struct proc *p;
1379 register struct flock_args *uap;
1380{
1381 register struct filedesc *fdp = p->p_fd;
1382 register struct file *fp;
1383 struct vnode *vp;
1384 struct flock lf;
1385 int error;
1287
1386
1387 mtx_lock(&Giant);
1388
1288 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1389 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1289 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1290 return (EBADF);
1291 if (fp->f_type != DTYPE_VNODE)
1292 return (EOPNOTSUPP);
1390 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
1391 error = EBADF;
1392 goto done2;
1393 }
1394 if (fp->f_type != DTYPE_VNODE) {
1395 error = EOPNOTSUPP;
1396 goto done2;
1397 }
1293 vp = (struct vnode *)fp->f_data;
1294 lf.l_whence = SEEK_SET;
1295 lf.l_start = 0;
1296 lf.l_len = 0;
1297 if (uap->how & LOCK_UN) {
1298 lf.l_type = F_UNLCK;
1299 fp->f_flag &= ~FHASLOCK;
1398 vp = (struct vnode *)fp->f_data;
1399 lf.l_whence = SEEK_SET;
1400 lf.l_start = 0;
1401 lf.l_len = 0;
1402 if (uap->how & LOCK_UN) {
1403 lf.l_type = F_UNLCK;
1404 fp->f_flag &= ~FHASLOCK;
1300 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1405 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1406 goto done2;
1301 }
1302 if (uap->how & LOCK_EX)
1303 lf.l_type = F_WRLCK;
1304 else if (uap->how & LOCK_SH)
1305 lf.l_type = F_RDLCK;
1407 }
1408 if (uap->how & LOCK_EX)
1409 lf.l_type = F_WRLCK;
1410 else if (uap->how & LOCK_SH)
1411 lf.l_type = F_RDLCK;
1306 else
1307 return (EBADF);
1412 else {
1413 error = EBADF;
1414 goto done2;
1415 }
1308 fp->f_flag |= FHASLOCK;
1309 if (uap->how & LOCK_NB)
1416 fp->f_flag |= FHASLOCK;
1417 if (uap->how & LOCK_NB)
1310 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1311 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1418 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
1419 else
1420 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
1421done2:
1422 mtx_unlock(&Giant);
1423 return (error);
1312}
1313
1314/*
1315 * File Descriptor pseudo-device driver (/dev/fd/).
1316 *
1317 * Opening minor device N dup()s the file (if any) connected to file
1318 * descriptor N belonging to the calling process. Note that this driver
1319 * consists of only the ``open()'' routine, because all subsequent

--- 264 unchanged lines hidden ---
1424}
1425
1426/*
1427 * File Descriptor pseudo-device driver (/dev/fd/).
1428 *
1429 * Opening minor device N dup()s the file (if any) connected to file
1430 * descriptor N belonging to the calling process. Note that this driver
1431 * consists of only the ``open()'' routine, because all subsequent

--- 264 unchanged lines hidden ---