xref: /titanic_41/usr/src/uts/common/syscall/ioctl.c (revision fd9cb95cbb2f626355a60efb9d02c5f0a33c10e6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/cred.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/ttold.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/mode.h>
48 #include <sys/proc.h>
49 #include <sys/uio.h>
50 #include <sys/kmem.h>
51 #include <sys/filio.h>
52 #include <sys/sunddi.h>
53 #include <sys/debug.h>
54 #include <sys/int_limits.h>
55 #include <sys/model.h>
56 
57 /*
58  * I/O control.
59  */
60 
61 int
62 ioctl(int fdes, int cmd, intptr_t arg)
63 {
64 	file_t *fp;
65 	int error = 0;
66 	vnode_t *vp;
67 	struct vattr vattr;
68 	int32_t flag;
69 	int rv = 0;
70 
71 	if ((fp = getf(fdes)) == NULL)
72 		return (set_errno(EBADF));
73 	vp = fp->f_vnode;
74 
75 	if (vp->v_type == VREG || vp->v_type == VDIR) {
76 		/*
77 		 * Handle these two ioctls for regular files and
78 		 * directories.  All others will usually be failed
79 		 * with ENOTTY by the VFS-dependent code.  System V
80 		 * always failed all ioctls on regular files, but SunOS
81 		 * supported these.
82 		 */
83 		switch (cmd) {
84 		case FIONREAD: {
85 			/*
86 			 * offset is int32_t because that is what FIONREAD
87 			 * is defined in terms of.  We cap at INT_MAX as in
88 			 * other cases for this ioctl.
89 			 */
90 			int32_t offset;
91 
92 			vattr.va_mask = AT_SIZE;
93 			error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred);
94 			if (error) {
95 				releasef(fdes);
96 				return (set_errno(error));
97 			}
98 			offset = MIN(vattr.va_size - fp->f_offset, INT_MAX);
99 			if (copyout(&offset, (caddr_t)arg, sizeof (offset))) {
100 				releasef(fdes);
101 				return (set_errno(EFAULT));
102 			}
103 			releasef(fdes);
104 			return (0);
105 			}
106 
107 		case FIONBIO:
108 			if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
109 				releasef(fdes);
110 				return (set_errno(EFAULT));
111 			}
112 			mutex_enter(&fp->f_tlock);
113 			if (flag)
114 				fp->f_flag |= FNDELAY;
115 			else
116 				fp->f_flag &= ~FNDELAY;
117 			mutex_exit(&fp->f_tlock);
118 			releasef(fdes);
119 			return (0);
120 
121 		default:
122 			break;
123 		}
124 	}
125 
126 	/*
127 	 * ioctl() now passes in the model information in some high bits.
128 	 */
129 	flag = fp->f_flag | get_udatamodel();
130 	error = VOP_IOCTL(fp->f_vnode, cmd, arg, flag, CRED(), &rv);
131 	if (error != 0) {
132 		releasef(fdes);
133 		return (set_errno(error));
134 	}
135 	switch (cmd) {
136 	case FIONBIO:
137 		if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
138 			releasef(fdes);
139 			return (set_errno(EFAULT));
140 		}
141 		mutex_enter(&fp->f_tlock);
142 		if (flag)
143 			fp->f_flag |= FNDELAY;
144 		else
145 			fp->f_flag &= ~FNDELAY;
146 		mutex_exit(&fp->f_tlock);
147 		break;
148 
149 	default:
150 		break;
151 	}
152 	releasef(fdes);
153 	return (rv);
154 }
155 
156 /*
157  * Old stty and gtty.  (Still.)
158  */
159 int
160 stty(int fdes, intptr_t arg)
161 {
162 	return (ioctl(fdes, TIOCSETP, arg));
163 }
164 
165 int
166 gtty(int fdes, intptr_t arg)
167 {
168 	return (ioctl(fdes, TIOCGETP, arg));
169 }
170