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