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