xref: /onnv-gate/usr/src/uts/common/syscall/ioctl.c (revision 5331:3047ad28a67b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/isa_defs.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/cred.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/ttold.h>
440Sstevel@tonic-gate #include <sys/vnode.h>
450Sstevel@tonic-gate #include <sys/file.h>
460Sstevel@tonic-gate #include <sys/mode.h>
470Sstevel@tonic-gate #include <sys/proc.h>
480Sstevel@tonic-gate #include <sys/uio.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/filio.h>
510Sstevel@tonic-gate #include <sys/sunddi.h>
520Sstevel@tonic-gate #include <sys/debug.h>
530Sstevel@tonic-gate #include <sys/int_limits.h>
540Sstevel@tonic-gate #include <sys/model.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * I/O control.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate int
ioctl(int fdes,int cmd,intptr_t arg)610Sstevel@tonic-gate ioctl(int fdes, int cmd, intptr_t arg)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	file_t *fp;
640Sstevel@tonic-gate 	int error = 0;
650Sstevel@tonic-gate 	vnode_t *vp;
660Sstevel@tonic-gate 	struct vattr vattr;
670Sstevel@tonic-gate 	int32_t flag;
680Sstevel@tonic-gate 	int rv = 0;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
710Sstevel@tonic-gate 		return (set_errno(EBADF));
720Sstevel@tonic-gate 	vp = fp->f_vnode;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (vp->v_type == VREG || vp->v_type == VDIR) {
750Sstevel@tonic-gate 		/*
760Sstevel@tonic-gate 		 * Handle these two ioctls for regular files and
770Sstevel@tonic-gate 		 * directories.  All others will usually be failed
780Sstevel@tonic-gate 		 * with ENOTTY by the VFS-dependent code.  System V
790Sstevel@tonic-gate 		 * always failed all ioctls on regular files, but SunOS
800Sstevel@tonic-gate 		 * supported these.
810Sstevel@tonic-gate 		 */
820Sstevel@tonic-gate 		switch (cmd) {
830Sstevel@tonic-gate 		case FIONREAD: {
840Sstevel@tonic-gate 			/*
850Sstevel@tonic-gate 			 * offset is int32_t because that is what FIONREAD
860Sstevel@tonic-gate 			 * is defined in terms of.  We cap at INT_MAX as in
870Sstevel@tonic-gate 			 * other cases for this ioctl.
880Sstevel@tonic-gate 			 */
890Sstevel@tonic-gate 			int32_t offset;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
92*5331Samw 			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL);
930Sstevel@tonic-gate 			if (error) {
940Sstevel@tonic-gate 				releasef(fdes);
950Sstevel@tonic-gate 				return (set_errno(error));
960Sstevel@tonic-gate 			}
970Sstevel@tonic-gate 			offset = MIN(vattr.va_size - fp->f_offset, INT_MAX);
980Sstevel@tonic-gate 			if (copyout(&offset, (caddr_t)arg, sizeof (offset))) {
990Sstevel@tonic-gate 				releasef(fdes);
1000Sstevel@tonic-gate 				return (set_errno(EFAULT));
1010Sstevel@tonic-gate 			}
1020Sstevel@tonic-gate 			releasef(fdes);
1030Sstevel@tonic-gate 			return (0);
1040Sstevel@tonic-gate 			}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		case FIONBIO:
1070Sstevel@tonic-gate 			if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
1080Sstevel@tonic-gate 				releasef(fdes);
1090Sstevel@tonic-gate 				return (set_errno(EFAULT));
1100Sstevel@tonic-gate 			}
1110Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
1120Sstevel@tonic-gate 			if (flag)
1130Sstevel@tonic-gate 				fp->f_flag |= FNDELAY;
1140Sstevel@tonic-gate 			else
1150Sstevel@tonic-gate 				fp->f_flag &= ~FNDELAY;
1160Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
1170Sstevel@tonic-gate 			releasef(fdes);
1180Sstevel@tonic-gate 			return (0);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		default:
1210Sstevel@tonic-gate 			break;
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * ioctl() now passes in the model information in some high bits.
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	flag = fp->f_flag | get_udatamodel();
129*5331Samw 	error = VOP_IOCTL(fp->f_vnode, cmd, arg, flag, CRED(), &rv, NULL);
1300Sstevel@tonic-gate 	if (error != 0) {
1310Sstevel@tonic-gate 		releasef(fdes);
1320Sstevel@tonic-gate 		return (set_errno(error));
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 	switch (cmd) {
1350Sstevel@tonic-gate 	case FIONBIO:
1360Sstevel@tonic-gate 		if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
1370Sstevel@tonic-gate 			releasef(fdes);
1380Sstevel@tonic-gate 			return (set_errno(EFAULT));
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
1410Sstevel@tonic-gate 		if (flag)
1420Sstevel@tonic-gate 			fp->f_flag |= FNDELAY;
1430Sstevel@tonic-gate 		else
1440Sstevel@tonic-gate 			fp->f_flag &= ~FNDELAY;
1450Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
1460Sstevel@tonic-gate 		break;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	default:
1490Sstevel@tonic-gate 		break;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 	releasef(fdes);
1520Sstevel@tonic-gate 	return (rv);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * Old stty and gtty.  (Still.)
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate int
stty(int fdes,intptr_t arg)1590Sstevel@tonic-gate stty(int fdes, intptr_t arg)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	return (ioctl(fdes, TIOCSETP, arg));
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate int
gtty(int fdes,intptr_t arg)1650Sstevel@tonic-gate gtty(int fdes, intptr_t arg)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	return (ioctl(fdes, TIOCGETP, arg));
1680Sstevel@tonic-gate }
169