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 #include <sys/param.h>
35 #include <sys/isa_defs.h>
36 #include <sys/types.h>
37 #include <sys/sysmacros.h>
38 #include <sys/cred.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/ttold.h>
42 #include <sys/vnode.h>
43 #include <sys/file.h>
44 #include <sys/mode.h>
45 #include <sys/proc.h>
46 #include <sys/uio.h>
47 #include <sys/kmem.h>
48 #include <sys/filio.h>
49 #include <sys/sunddi.h>
50 #include <sys/debug.h>
51 #include <sys/int_limits.h>
52 #include <sys/model.h>
53
54 /*
55 * I/O control.
56 */
57
58 int
ioctl(int fdes,int cmd,intptr_t arg)59 ioctl(int fdes, int cmd, intptr_t arg)
60 {
61 file_t *fp;
62 int error = 0;
63 vnode_t *vp;
64 struct vattr vattr;
65 int32_t flag;
66 int rv = 0;
67
68 if ((fp = getf(fdes)) == NULL)
69 return (set_errno(EBADF));
70 vp = fp->f_vnode;
71
72 if (vp->v_type == VREG || vp->v_type == VDIR) {
73 /*
74 * Handle these two ioctls for regular files and
75 * directories. All others will usually be failed
76 * with ENOTTY by the VFS-dependent code. System V
77 * always failed all ioctls on regular files, but SunOS
78 * supported these.
79 */
80 switch (cmd) {
81 case FIONREAD: {
82 /*
83 * offset is int32_t because that is what FIONREAD
84 * is defined in terms of. We cap at INT_MAX as in
85 * other cases for this ioctl.
86 */
87 int32_t offset;
88
89 vattr.va_mask = AT_SIZE;
90 error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL);
91 if (error) {
92 releasef(fdes);
93 return (set_errno(error));
94 }
95 offset = MIN(vattr.va_size - fp->f_offset, INT_MAX);
96 if (copyout(&offset, (caddr_t)arg, sizeof (offset))) {
97 releasef(fdes);
98 return (set_errno(EFAULT));
99 }
100 releasef(fdes);
101 return (0);
102 }
103
104 case FIONBIO:
105 if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
106 releasef(fdes);
107 return (set_errno(EFAULT));
108 }
109 mutex_enter(&fp->f_tlock);
110 if (flag)
111 fp->f_flag |= FNDELAY;
112 else
113 fp->f_flag &= ~FNDELAY;
114 mutex_exit(&fp->f_tlock);
115 releasef(fdes);
116 return (0);
117
118 default:
119 break;
120 }
121 }
122
123 /*
124 * ioctl() now passes in the model information in some high bits.
125 */
126 flag = fp->f_flag | get_udatamodel();
127 error = VOP_IOCTL(fp->f_vnode, cmd, arg, flag, CRED(), &rv, NULL);
128 if (error != 0) {
129 releasef(fdes);
130 return (set_errno(error));
131 }
132 switch (cmd) {
133 case FIONBIO:
134 if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
135 releasef(fdes);
136 return (set_errno(EFAULT));
137 }
138 mutex_enter(&fp->f_tlock);
139 if (flag)
140 fp->f_flag |= FNDELAY;
141 else
142 fp->f_flag &= ~FNDELAY;
143 mutex_exit(&fp->f_tlock);
144 break;
145
146 default:
147 break;
148 }
149 releasef(fdes);
150 return (rv);
151 }
152
153 /*
154 * Old stty and gtty. (Still.)
155 */
156 int
stty(int fdes,intptr_t arg)157 stty(int fdes, intptr_t arg)
158 {
159 return (ioctl(fdes, TIOCSETP, arg));
160 }
161
162 int
gtty(int fdes,intptr_t arg)163 gtty(int fdes, intptr_t arg)
164 {
165 return (ioctl(fdes, TIOCGETP, arg));
166 }
167