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