Home | History | Annotate | Line # | Download | only in kern
sys_descrip.c revision 1.1
      1  1.1  ad /*	$NetBSD: sys_descrip.c,v 1.1 2008/03/21 21:53:35 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.1  ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.1  ad  * All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * Redistribution and use in source and binary forms, with or without
      8  1.1  ad  * modification, are permitted provided that the following conditions
      9  1.1  ad  * are met:
     10  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  ad  *    documentation and/or other materials provided with the distribution.
     15  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     16  1.1  ad  *    must display the following acknowledgement:
     17  1.1  ad  *	This product includes software developed by the NetBSD
     18  1.1  ad  *	Foundation, Inc. and its contributors.
     19  1.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.1  ad  *    contributors may be used to endorse or promote products derived
     21  1.1  ad  *    from this software without specific prior written permission.
     22  1.1  ad  *
     23  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  ad  */
     35  1.1  ad 
     36  1.1  ad /*
     37  1.1  ad  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     38  1.1  ad  *	The Regents of the University of California.  All rights reserved.
     39  1.1  ad  * (c) UNIX System Laboratories, Inc.
     40  1.1  ad  * All or some portions of this file are derived from material licensed
     41  1.1  ad  * to the University of California by American Telephone and Telegraph
     42  1.1  ad  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     43  1.1  ad  * the permission of UNIX System Laboratories, Inc.
     44  1.1  ad  *
     45  1.1  ad  * Redistribution and use in source and binary forms, with or without
     46  1.1  ad  * modification, are permitted provided that the following conditions
     47  1.1  ad  * are met:
     48  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     49  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     50  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     51  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     52  1.1  ad  *    documentation and/or other materials provided with the distribution.
     53  1.1  ad  * 3. Neither the name of the University nor the names of its contributors
     54  1.1  ad  *    may be used to endorse or promote products derived from this software
     55  1.1  ad  *    without specific prior written permission.
     56  1.1  ad  *
     57  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  1.1  ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  1.1  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  1.1  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  1.1  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  1.1  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  1.1  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  1.1  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  1.1  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  1.1  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  1.1  ad  * SUCH DAMAGE.
     68  1.1  ad  *
     69  1.1  ad  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     70  1.1  ad  */
     71  1.1  ad 
     72  1.1  ad /*
     73  1.1  ad  * System calls on descriptors.
     74  1.1  ad  */
     75  1.1  ad 
     76  1.1  ad #include <sys/cdefs.h>
     77  1.1  ad __KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.1 2008/03/21 21:53:35 ad Exp $");
     78  1.1  ad 
     79  1.1  ad #include <sys/param.h>
     80  1.1  ad #include <sys/systm.h>
     81  1.1  ad #include <sys/filedesc.h>
     82  1.1  ad #include <sys/kernel.h>
     83  1.1  ad #include <sys/vnode.h>
     84  1.1  ad #include <sys/proc.h>
     85  1.1  ad #include <sys/file.h>
     86  1.1  ad #include <sys/namei.h>
     87  1.1  ad #include <sys/socket.h>
     88  1.1  ad #include <sys/socketvar.h>
     89  1.1  ad #include <sys/stat.h>
     90  1.1  ad #include <sys/ioctl.h>
     91  1.1  ad #include <sys/fcntl.h>
     92  1.1  ad #include <sys/malloc.h>
     93  1.1  ad #include <sys/pool.h>
     94  1.1  ad #include <sys/syslog.h>
     95  1.1  ad #include <sys/unistd.h>
     96  1.1  ad #include <sys/resourcevar.h>
     97  1.1  ad #include <sys/conf.h>
     98  1.1  ad #include <sys/event.h>
     99  1.1  ad #include <sys/kauth.h>
    100  1.1  ad #include <sys/atomic.h>
    101  1.1  ad #include <sys/mount.h>
    102  1.1  ad #include <sys/syscallargs.h>
    103  1.1  ad 
    104  1.1  ad /*
    105  1.1  ad  * Duplicate a file descriptor.
    106  1.1  ad  */
    107  1.1  ad int
    108  1.1  ad sys_dup(struct lwp *l, const struct sys_dup_args *uap, register_t *retval)
    109  1.1  ad {
    110  1.1  ad 	/* {
    111  1.1  ad 		syscallarg(int)	fd;
    112  1.1  ad 	} */
    113  1.1  ad 	int new, error, old;
    114  1.1  ad 	file_t *fp;
    115  1.1  ad 
    116  1.1  ad 	old = SCARG(uap, fd);
    117  1.1  ad 
    118  1.1  ad 	if ((fp = fd_getfile(old)) == NULL) {
    119  1.1  ad 		return EBADF;
    120  1.1  ad 	}
    121  1.1  ad 	error = fd_dup(fp, 0, &new, 0);
    122  1.1  ad 	fd_putfile(old);
    123  1.1  ad 	*retval = new;
    124  1.1  ad 	return error;
    125  1.1  ad }
    126  1.1  ad 
    127  1.1  ad /*
    128  1.1  ad  * Duplicate a file descriptor to a particular value.
    129  1.1  ad  */
    130  1.1  ad int
    131  1.1  ad sys_dup2(struct lwp *l, const struct sys_dup2_args *uap, register_t *retval)
    132  1.1  ad {
    133  1.1  ad 	/* {
    134  1.1  ad 		syscallarg(int)	from;
    135  1.1  ad 		syscallarg(int)	to;
    136  1.1  ad 	} */
    137  1.1  ad 	int old, new, error;
    138  1.1  ad 	file_t *fp;
    139  1.1  ad 
    140  1.1  ad 	old = SCARG(uap, from);
    141  1.1  ad 	new = SCARG(uap, to);
    142  1.1  ad 
    143  1.1  ad 	if ((fp = fd_getfile(old)) == NULL) {
    144  1.1  ad 		return EBADF;
    145  1.1  ad 	}
    146  1.1  ad 	if ((u_int)new >= curproc->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    147  1.1  ad 	    (u_int)new >= maxfiles) {
    148  1.1  ad 		error = EBADF;
    149  1.1  ad 	} else if (old == new) {
    150  1.1  ad 		error = 0;
    151  1.1  ad 	} else {
    152  1.1  ad 		error = fd_dup2(fp, new);
    153  1.1  ad 	}
    154  1.1  ad 	fd_putfile(old);
    155  1.1  ad 	*retval = new;
    156  1.1  ad 
    157  1.1  ad 	return 0;
    158  1.1  ad }
    159  1.1  ad 
    160  1.1  ad /*
    161  1.1  ad  * fcntl call which is being passed to the file's fs.
    162  1.1  ad  */
    163  1.1  ad static int
    164  1.1  ad fcntl_forfs(int fd, file_t *fp, int cmd, void *arg)
    165  1.1  ad {
    166  1.1  ad 	int		error;
    167  1.1  ad 	u_int		size;
    168  1.1  ad 	void		*data, *memp;
    169  1.1  ad #define STK_PARAMS	128
    170  1.1  ad 	char		stkbuf[STK_PARAMS];
    171  1.1  ad 
    172  1.1  ad 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
    173  1.1  ad 		return (EBADF);
    174  1.1  ad 
    175  1.1  ad 	/*
    176  1.1  ad 	 * Interpret high order word to find amount of data to be
    177  1.1  ad 	 * copied to/from the user's address space.
    178  1.1  ad 	 */
    179  1.1  ad 	size = (size_t)F_PARAM_LEN(cmd);
    180  1.1  ad 	if (size > F_PARAM_MAX)
    181  1.1  ad 		return (EINVAL);
    182  1.1  ad 	memp = NULL;
    183  1.1  ad 	if (size > sizeof(stkbuf)) {
    184  1.1  ad 		memp = kmem_alloc(size, KM_SLEEP);
    185  1.1  ad 		data = memp;
    186  1.1  ad 	} else
    187  1.1  ad 		data = stkbuf;
    188  1.1  ad 	if (cmd & F_FSIN) {
    189  1.1  ad 		if (size) {
    190  1.1  ad 			error = copyin(arg, data, size);
    191  1.1  ad 			if (error) {
    192  1.1  ad 				if (memp)
    193  1.1  ad 					kmem_free(memp, size);
    194  1.1  ad 				return (error);
    195  1.1  ad 			}
    196  1.1  ad 		} else
    197  1.1  ad 			*(void **)data = arg;
    198  1.1  ad 	} else if ((cmd & F_FSOUT) != 0 && size != 0) {
    199  1.1  ad 		/*
    200  1.1  ad 		 * Zero the buffer so the user always
    201  1.1  ad 		 * gets back something deterministic.
    202  1.1  ad 		 */
    203  1.1  ad 		memset(data, 0, size);
    204  1.1  ad 	} else if (cmd & F_FSVOID)
    205  1.1  ad 		*(void **)data = arg;
    206  1.1  ad 
    207  1.1  ad 
    208  1.1  ad 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data);
    209  1.1  ad 
    210  1.1  ad 	/*
    211  1.1  ad 	 * Copy any data to user, size was
    212  1.1  ad 	 * already set and checked above.
    213  1.1  ad 	 */
    214  1.1  ad 	if (error == 0 && (cmd & F_FSOUT) && size)
    215  1.1  ad 		error = copyout(data, arg, size);
    216  1.1  ad 	if (memp)
    217  1.1  ad 		kmem_free(memp, size);
    218  1.1  ad 	return (error);
    219  1.1  ad }
    220  1.1  ad 
    221  1.1  ad int
    222  1.1  ad do_fcntl_lock(int fd, int cmd, struct flock *fl)
    223  1.1  ad {
    224  1.1  ad 	file_t *fp;
    225  1.1  ad 	vnode_t *vp;
    226  1.1  ad 	proc_t *p;
    227  1.1  ad 	int error, flg;
    228  1.1  ad 
    229  1.1  ad 	if ((fp = fd_getfile(fd)) == NULL)
    230  1.1  ad 		return EBADF;
    231  1.1  ad 	if (fp->f_type != DTYPE_VNODE) {
    232  1.1  ad 		fd_putfile(fd);
    233  1.1  ad 		return EINVAL;
    234  1.1  ad 	}
    235  1.1  ad 	vp = fp->f_data;
    236  1.1  ad 	if (fl->l_whence == SEEK_CUR)
    237  1.1  ad 		fl->l_start += fp->f_offset;
    238  1.1  ad 
    239  1.1  ad 	flg = F_POSIX;
    240  1.1  ad 	p = curproc;
    241  1.1  ad 
    242  1.1  ad 	switch (cmd) {
    243  1.1  ad 	case F_SETLKW:
    244  1.1  ad 		flg |= F_WAIT;
    245  1.1  ad 		/* Fall into F_SETLK */
    246  1.1  ad 
    247  1.1  ad 	case F_SETLK:
    248  1.1  ad 		switch (fl->l_type) {
    249  1.1  ad 		case F_RDLCK:
    250  1.1  ad 			if ((fp->f_flag & FREAD) == 0) {
    251  1.1  ad 				error = EBADF;
    252  1.1  ad 				break;
    253  1.1  ad 			}
    254  1.1  ad 			if ((p->p_flag & PK_ADVLOCK) == 0) {
    255  1.1  ad 				mutex_enter(&p->p_mutex);
    256  1.1  ad 				p->p_flag |= PK_ADVLOCK;
    257  1.1  ad 				mutex_exit(&p->p_mutex);
    258  1.1  ad 			}
    259  1.1  ad 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
    260  1.1  ad 			break;
    261  1.1  ad 
    262  1.1  ad 		case F_WRLCK:
    263  1.1  ad 			if ((fp->f_flag & FWRITE) == 0) {
    264  1.1  ad 				error = EBADF;
    265  1.1  ad 				break;
    266  1.1  ad 			}
    267  1.1  ad 			if ((p->p_flag & PK_ADVLOCK) == 0) {
    268  1.1  ad 				mutex_enter(&p->p_mutex);
    269  1.1  ad 				p->p_flag |= PK_ADVLOCK;
    270  1.1  ad 				mutex_exit(&p->p_mutex);
    271  1.1  ad 			}
    272  1.1  ad 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
    273  1.1  ad 			break;
    274  1.1  ad 
    275  1.1  ad 		case F_UNLCK:
    276  1.1  ad 			error = VOP_ADVLOCK(vp, p, F_UNLCK, fl, F_POSIX);
    277  1.1  ad 			break;
    278  1.1  ad 
    279  1.1  ad 		default:
    280  1.1  ad 			error = EINVAL;
    281  1.1  ad 			break;
    282  1.1  ad 		}
    283  1.1  ad 		break;
    284  1.1  ad 
    285  1.1  ad 	case F_GETLK:
    286  1.1  ad 		if (fl->l_type != F_RDLCK &&
    287  1.1  ad 		    fl->l_type != F_WRLCK &&
    288  1.1  ad 		    fl->l_type != F_UNLCK) {
    289  1.1  ad 			error = EINVAL;
    290  1.1  ad 			break;
    291  1.1  ad 		}
    292  1.1  ad 		error = VOP_ADVLOCK(vp, p, F_GETLK, fl, F_POSIX);
    293  1.1  ad 		break;
    294  1.1  ad 
    295  1.1  ad 	default:
    296  1.1  ad 		error = EINVAL;
    297  1.1  ad 		break;
    298  1.1  ad 	}
    299  1.1  ad 
    300  1.1  ad 	fd_putfile(fd);
    301  1.1  ad 	return error;
    302  1.1  ad }
    303  1.1  ad 
    304  1.1  ad /*
    305  1.1  ad  * The file control system call.
    306  1.1  ad  */
    307  1.1  ad int
    308  1.1  ad sys_fcntl(struct lwp *l, const struct sys_fcntl_args *uap, register_t *retval)
    309  1.1  ad {
    310  1.1  ad 	/* {
    311  1.1  ad 		syscallarg(int)		fd;
    312  1.1  ad 		syscallarg(int)		cmd;
    313  1.1  ad 		syscallarg(void *)	arg;
    314  1.1  ad 	} */
    315  1.1  ad 	int fd, i, tmp, error, cmd, newmin;
    316  1.1  ad 	filedesc_t *fdp;
    317  1.1  ad 	file_t *fp;
    318  1.1  ad 	fdfile_t *ff;
    319  1.1  ad 	proc_t *p;
    320  1.1  ad 	struct flock fl;
    321  1.1  ad 
    322  1.1  ad 	p = l->l_proc;
    323  1.1  ad 	fd = SCARG(uap, fd);
    324  1.1  ad 	cmd = SCARG(uap, cmd);
    325  1.1  ad 	fdp = p->p_fd;
    326  1.1  ad 	error = 0;
    327  1.1  ad 
    328  1.1  ad 	switch (cmd) {
    329  1.1  ad 	case F_CLOSEM:
    330  1.1  ad 		if (fd < 0)
    331  1.1  ad 			return EBADF;
    332  1.1  ad 		while ((i = fdp->fd_lastfile) >= fd) {
    333  1.1  ad 			if (fd_getfile(i) == NULL) {
    334  1.1  ad 				/* Another thread has updated. */
    335  1.1  ad 				continue;
    336  1.1  ad 			}
    337  1.1  ad 			fd_close(i);
    338  1.1  ad 		}
    339  1.1  ad 		return 0;
    340  1.1  ad 
    341  1.1  ad 	case F_MAXFD:
    342  1.1  ad 		*retval = fdp->fd_lastfile;
    343  1.1  ad 		return 0;
    344  1.1  ad 
    345  1.1  ad 	case F_SETLKW:
    346  1.1  ad 	case F_SETLK:
    347  1.1  ad 	case F_GETLK:
    348  1.1  ad 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
    349  1.1  ad 		if (error)
    350  1.1  ad 			return error;
    351  1.1  ad 		error = do_fcntl_lock(fd, cmd, &fl);
    352  1.1  ad 		if (cmd == F_GETLK && error == 0)
    353  1.1  ad 			error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
    354  1.1  ad 		return error;
    355  1.1  ad 
    356  1.1  ad 	default:
    357  1.1  ad 		/* Handled below */
    358  1.1  ad 		break;
    359  1.1  ad 	}
    360  1.1  ad 
    361  1.1  ad 	if ((fp = fd_getfile(fd)) == NULL)
    362  1.1  ad 		return (EBADF);
    363  1.1  ad 	ff = fdp->fd_ofiles[fd];
    364  1.1  ad 
    365  1.1  ad 	if ((cmd & F_FSCTL)) {
    366  1.1  ad 		error = fcntl_forfs(fd, fp, cmd, SCARG(uap, arg));
    367  1.1  ad 		fd_putfile(fd);
    368  1.1  ad 		return error;
    369  1.1  ad 	}
    370  1.1  ad 
    371  1.1  ad 	switch (cmd) {
    372  1.1  ad 	case F_DUPFD:
    373  1.1  ad 		newmin = (long)SCARG(uap, arg);
    374  1.1  ad 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    375  1.1  ad 		    (u_int)newmin >= maxfiles) {
    376  1.1  ad 			fd_putfile(fd);
    377  1.1  ad 			return EINVAL;
    378  1.1  ad 		}
    379  1.1  ad 		error = fd_dup(fp, newmin, &i, 0);
    380  1.1  ad 		*retval = i;
    381  1.1  ad 		break;
    382  1.1  ad 
    383  1.1  ad 	case F_GETFD:
    384  1.1  ad 		*retval = ff->ff_exclose;
    385  1.1  ad 		break;
    386  1.1  ad 
    387  1.1  ad 	case F_SETFD:
    388  1.1  ad 		if ((long)SCARG(uap, arg) & 1) {
    389  1.1  ad 			ff->ff_exclose = 1;
    390  1.1  ad 			fdp->fd_exclose = 1;
    391  1.1  ad 		} else {
    392  1.1  ad 			ff->ff_exclose = 0;
    393  1.1  ad 		}
    394  1.1  ad 		break;
    395  1.1  ad 
    396  1.1  ad 	case F_GETFL:
    397  1.1  ad 		*retval = OFLAGS(fp->f_flag);
    398  1.1  ad 		break;
    399  1.1  ad 
    400  1.1  ad 	case F_SETFL:
    401  1.1  ad 		/* XXX not guaranteed to be atomic. */
    402  1.1  ad 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    403  1.1  ad 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp);
    404  1.1  ad 		if (error)
    405  1.1  ad 			break;
    406  1.1  ad 		i = tmp ^ fp->f_flag;
    407  1.1  ad 		if (i & FNONBLOCK) {
    408  1.1  ad 			int flgs = tmp & FNONBLOCK;
    409  1.1  ad 			error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs);
    410  1.1  ad 			if (error) {
    411  1.1  ad 				(*fp->f_ops->fo_fcntl)(fp, F_SETFL,
    412  1.1  ad 				    &fp->f_flag);
    413  1.1  ad 				break;
    414  1.1  ad 			}
    415  1.1  ad 		}
    416  1.1  ad 		if (i & FASYNC) {
    417  1.1  ad 			int flgs = tmp & FASYNC;
    418  1.1  ad 			error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs);
    419  1.1  ad 			if (error) {
    420  1.1  ad 				if (i & FNONBLOCK) {
    421  1.1  ad 					tmp = fp->f_flag & FNONBLOCK;
    422  1.1  ad 					(void)(*fp->f_ops->fo_ioctl)(fp,
    423  1.1  ad 						FIONBIO, &tmp);
    424  1.1  ad 				}
    425  1.1  ad 				(*fp->f_ops->fo_fcntl)(fp, F_SETFL,
    426  1.1  ad 				    &fp->f_flag);
    427  1.1  ad 				break;
    428  1.1  ad 			}
    429  1.1  ad 		}
    430  1.1  ad 		fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
    431  1.1  ad 		break;
    432  1.1  ad 
    433  1.1  ad 	case F_GETOWN:
    434  1.1  ad 		error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp);
    435  1.1  ad 		*retval = tmp;
    436  1.1  ad 		break;
    437  1.1  ad 
    438  1.1  ad 	case F_SETOWN:
    439  1.1  ad 		tmp = (int)(intptr_t) SCARG(uap, arg);
    440  1.1  ad 		error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp);
    441  1.1  ad 		break;
    442  1.1  ad 
    443  1.1  ad 	default:
    444  1.1  ad 		error = EINVAL;
    445  1.1  ad 	}
    446  1.1  ad 
    447  1.1  ad 	fd_putfile(fd);
    448  1.1  ad 	return (error);
    449  1.1  ad }
    450  1.1  ad 
    451  1.1  ad /*
    452  1.1  ad  * Close a file descriptor.
    453  1.1  ad  */
    454  1.1  ad int
    455  1.1  ad sys_close(struct lwp *l, const struct sys_close_args *uap, register_t *retval)
    456  1.1  ad {
    457  1.1  ad 	/* {
    458  1.1  ad 		syscallarg(int)	fd;
    459  1.1  ad 	} */
    460  1.1  ad 
    461  1.1  ad 	if (fd_getfile(SCARG(uap, fd)) == NULL) {
    462  1.1  ad 		return EBADF;
    463  1.1  ad 	}
    464  1.1  ad 	return fd_close(SCARG(uap, fd));
    465  1.1  ad }
    466  1.1  ad 
    467  1.1  ad /*
    468  1.1  ad  * Return status information about a file descriptor.
    469  1.1  ad  * Common function for compat code.
    470  1.1  ad  */
    471  1.1  ad int
    472  1.1  ad do_sys_fstat(int fd, struct stat *sb)
    473  1.1  ad {
    474  1.1  ad 	file_t *fp;
    475  1.1  ad 	int error;
    476  1.1  ad 
    477  1.1  ad 	if ((fp = fd_getfile(fd)) == NULL) {
    478  1.1  ad 		return EBADF;
    479  1.1  ad 	}
    480  1.1  ad 	error = (*fp->f_ops->fo_stat)(fp, sb);
    481  1.1  ad 	fd_putfile(fd);
    482  1.1  ad 
    483  1.1  ad 	return error;
    484  1.1  ad }
    485  1.1  ad 
    486  1.1  ad /*
    487  1.1  ad  * Return status information about a file descriptor.
    488  1.1  ad  */
    489  1.1  ad int
    490  1.1  ad sys___fstat30(struct lwp *l, const struct sys___fstat30_args *uap,
    491  1.1  ad 	      register_t *retval)
    492  1.1  ad {
    493  1.1  ad 	/* {
    494  1.1  ad 		syscallarg(int)			fd;
    495  1.1  ad 		syscallarg(struct stat *)	sb;
    496  1.1  ad 	} */
    497  1.1  ad 	struct stat sb;
    498  1.1  ad 	int error;
    499  1.1  ad 
    500  1.1  ad 	error = do_sys_fstat(SCARG(uap, fd), &sb);
    501  1.1  ad 	if (error == 0) {
    502  1.1  ad 		error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
    503  1.1  ad 	}
    504  1.1  ad 	return error;
    505  1.1  ad }
    506  1.1  ad 
    507  1.1  ad /*
    508  1.1  ad  * Return pathconf information about a file descriptor.
    509  1.1  ad  */
    510  1.1  ad int
    511  1.1  ad sys_fpathconf(struct lwp *l, const struct sys_fpathconf_args *uap,
    512  1.1  ad 	      register_t *retval)
    513  1.1  ad {
    514  1.1  ad 	/* {
    515  1.1  ad 		syscallarg(int)	fd;
    516  1.1  ad 		syscallarg(int)	name;
    517  1.1  ad 	} */
    518  1.1  ad 	int fd, error;
    519  1.1  ad 	file_t *fp;
    520  1.1  ad 
    521  1.1  ad 	fd = SCARG(uap, fd);
    522  1.1  ad 	error = 0;
    523  1.1  ad 
    524  1.1  ad 	if ((fp = fd_getfile(fd)) == NULL) {
    525  1.1  ad 		return (EBADF);
    526  1.1  ad 	}
    527  1.1  ad 	switch (fp->f_type) {
    528  1.1  ad 	case DTYPE_SOCKET:
    529  1.1  ad 	case DTYPE_PIPE:
    530  1.1  ad 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    531  1.1  ad 			error = EINVAL;
    532  1.1  ad 		else
    533  1.1  ad 			*retval = PIPE_BUF;
    534  1.1  ad 		break;
    535  1.1  ad 
    536  1.1  ad 	case DTYPE_VNODE:
    537  1.1  ad 		error = VOP_PATHCONF(fp->f_data, SCARG(uap, name), retval);
    538  1.1  ad 		break;
    539  1.1  ad 
    540  1.1  ad 	case DTYPE_KQUEUE:
    541  1.1  ad 		error = EINVAL;
    542  1.1  ad 		break;
    543  1.1  ad 
    544  1.1  ad 	default:
    545  1.1  ad 		error = EOPNOTSUPP;
    546  1.1  ad 		break;
    547  1.1  ad 	}
    548  1.1  ad 
    549  1.1  ad 	fd_putfile(fd);
    550  1.1  ad 	return (error);
    551  1.1  ad }
    552  1.1  ad 
    553  1.1  ad /*
    554  1.1  ad  * Apply an advisory lock on a file descriptor.
    555  1.1  ad  *
    556  1.1  ad  * Just attempt to get a record lock of the requested type on
    557  1.1  ad  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
    558  1.1  ad  */
    559  1.1  ad /* ARGSUSED */
    560  1.1  ad int
    561  1.1  ad sys_flock(struct lwp *l, const struct sys_flock_args *uap, register_t *retval)
    562  1.1  ad {
    563  1.1  ad 	/* {
    564  1.1  ad 		syscallarg(int)	fd;
    565  1.1  ad 		syscallarg(int)	how;
    566  1.1  ad 	} */
    567  1.1  ad 	int fd, how, error;
    568  1.1  ad 	file_t *fp;
    569  1.1  ad 	vnode_t	*vp;
    570  1.1  ad 	struct flock lf;
    571  1.1  ad 	proc_t *p;
    572  1.1  ad 
    573  1.1  ad 	fd = SCARG(uap, fd);
    574  1.1  ad 	how = SCARG(uap, how);
    575  1.1  ad 	error = 0;
    576  1.1  ad 
    577  1.1  ad 	if ((fp = fd_getfile(fd)) == NULL) {
    578  1.1  ad 		return EBADF;
    579  1.1  ad 	}
    580  1.1  ad 	if (fp->f_type != DTYPE_VNODE) {
    581  1.1  ad 		fd_putfile(fd);
    582  1.1  ad 		return EOPNOTSUPP;
    583  1.1  ad 	}
    584  1.1  ad 
    585  1.1  ad 	vp = fp->f_data;
    586  1.1  ad 	lf.l_whence = SEEK_SET;
    587  1.1  ad 	lf.l_start = 0;
    588  1.1  ad 	lf.l_len = 0;
    589  1.1  ad 	if (how & LOCK_UN) {
    590  1.1  ad 		lf.l_type = F_UNLCK;
    591  1.1  ad 		atomic_and_uint(&fp->f_flag, ~FHASLOCK);
    592  1.1  ad 		error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
    593  1.1  ad 		fd_putfile(fd);
    594  1.1  ad 		return error;
    595  1.1  ad 	}
    596  1.1  ad 	if (how & LOCK_EX) {
    597  1.1  ad 		lf.l_type = F_WRLCK;
    598  1.1  ad 	} else if (how & LOCK_SH) {
    599  1.1  ad 		lf.l_type = F_RDLCK;
    600  1.1  ad 	} else {
    601  1.1  ad 		fd_putfile(fd);
    602  1.1  ad 		return EINVAL;
    603  1.1  ad 	}
    604  1.1  ad 	atomic_or_uint(&fp->f_flag, FHASLOCK);
    605  1.1  ad 	p = curproc;
    606  1.1  ad 	if (how & LOCK_NB) {
    607  1.1  ad 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
    608  1.1  ad 	} else {
    609  1.1  ad 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
    610  1.1  ad 	}
    611  1.1  ad 	fd_putfile(fd);
    612  1.1  ad 	return error;
    613  1.1  ad }
    614  1.1  ad 
    615  1.1  ad int
    616  1.1  ad do_posix_fadvise(int fd, off_t offset, off_t len, int advice)
    617  1.1  ad {
    618  1.1  ad 	file_t *fp;
    619  1.1  ad 	int error;
    620  1.1  ad 
    621  1.1  ad 	if ((fp = fd_getfile(fd)) == NULL) {
    622  1.1  ad 		return EBADF;
    623  1.1  ad 	}
    624  1.1  ad 	if (fp->f_type != DTYPE_VNODE) {
    625  1.1  ad 		if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
    626  1.1  ad 			error = ESPIPE;
    627  1.1  ad 		} else {
    628  1.1  ad 			error = EOPNOTSUPP;
    629  1.1  ad 		}
    630  1.1  ad 		fd_putfile(fd);
    631  1.1  ad 		return error;
    632  1.1  ad 	}
    633  1.1  ad 
    634  1.1  ad 	switch (advice) {
    635  1.1  ad 	case POSIX_FADV_NORMAL:
    636  1.1  ad 	case POSIX_FADV_RANDOM:
    637  1.1  ad 	case POSIX_FADV_SEQUENTIAL:
    638  1.1  ad 		KASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
    639  1.1  ad 		KASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
    640  1.1  ad 		KASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
    641  1.1  ad 
    642  1.1  ad 		/*
    643  1.1  ad 		 * We ignore offset and size.  must lock the file to
    644  1.1  ad 		 * do this, as f_advice is sub-word sized.
    645  1.1  ad 		 */
    646  1.1  ad 		mutex_enter(&fp->f_lock);
    647  1.1  ad 		fp->f_advice = (u_char)advice;
    648  1.1  ad 		mutex_exit(&fp->f_lock);
    649  1.1  ad 		error = 0;
    650  1.1  ad 		break;
    651  1.1  ad 
    652  1.1  ad 	case POSIX_FADV_WILLNEED:
    653  1.1  ad 	case POSIX_FADV_DONTNEED:
    654  1.1  ad 	case POSIX_FADV_NOREUSE:
    655  1.1  ad 		/* Not implemented yet. */
    656  1.1  ad 		error = 0;
    657  1.1  ad 		break;
    658  1.1  ad 	default:
    659  1.1  ad 		error = EINVAL;
    660  1.1  ad 		break;
    661  1.1  ad 	}
    662  1.1  ad 
    663  1.1  ad 	fd_putfile(fd);
    664  1.1  ad 	return error;
    665  1.1  ad }
    666  1.1  ad 
    667  1.1  ad int
    668  1.1  ad sys___posix_fadvise50(struct lwp *l,
    669  1.1  ad 		      const struct sys___posix_fadvise50_args *uap,
    670  1.1  ad 		      register_t *retval)
    671  1.1  ad {
    672  1.1  ad 	/* {
    673  1.1  ad 		syscallarg(int) fd;
    674  1.1  ad 		syscallarg(int) pad;
    675  1.1  ad 		syscallarg(off_t) offset;
    676  1.1  ad 		syscallarg(off_t) len;
    677  1.1  ad 		syscallarg(int) advice;
    678  1.1  ad 	} */
    679  1.1  ad 
    680  1.1  ad 	return do_posix_fadvise(SCARG(uap, fd), SCARG(uap, offset),
    681  1.1  ad 	    SCARG(uap, len), SCARG(uap, advice));
    682  1.1  ad }
    683