svr4_ioctl.c revision 89319
1105197Ssam/*
2105197Ssam * Copyright (c) 1998 Mark Newton
3105197Ssam * Copyright (c) 1994 Christos Zoulas
4139823Simp * All rights reserved.
5105197Ssam *
6105197Ssam * Redistribution and use in source and binary forms, with or without
7105197Ssam * modification, are permitted provided that the following conditions
8105197Ssam * are met:
9105197Ssam * 1. Redistributions of source code must retain the above copyright
10105197Ssam *    notice, this list of conditions and the following disclaimer.
11105197Ssam * 2. Redistributions in binary form must reproduce the above copyright
12105197Ssam *    notice, this list of conditions and the following disclaimer in the
13105197Ssam *    documentation and/or other materials provided with the distribution.
14105197Ssam * 3. The name of the author may not be used to endorse or promote products
15105197Ssam *    derived from this software without specific prior written permission
16105197Ssam *
17105197Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18105197Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19105197Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20105197Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21105197Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22105197Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23105197Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24105197Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25105197Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26105197Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27105197Ssam *
28105197Ssam * $FreeBSD: head/sys/compat/svr4/svr4_ioctl.c 89319 2002-01-14 00:13:45Z alfred $
29105197Ssam */
30105197Ssam
31105197Ssam#include <sys/param.h>
32105197Ssam#include <sys/proc.h>
33105197Ssam#include <sys/file.h>
34105197Ssam#include <sys/filedesc.h>
35105197Ssam#include <sys/fcntl.h>
36105197Ssam#include <sys/socket.h>
37105197Ssam#include <sys/socketvar.h>
38105197Ssam#include <sys/systm.h>
39105197Ssam
40105197Ssam#include <compat/svr4/svr4.h>
41105197Ssam#include <compat/svr4/svr4_types.h>
42105197Ssam#include <compat/svr4/svr4_util.h>
43105197Ssam#include <compat/svr4/svr4_signal.h>
44253081Sae#include <compat/svr4/svr4_proto.h>
45105197Ssam#include <compat/svr4/svr4_stropts.h>
46253081Sae#include <compat/svr4/svr4_ioctl.h>
47253081Sae#include <compat/svr4/svr4_termios.h>
48253081Sae#include <compat/svr4/svr4_ttold.h>
49253081Sae#include <compat/svr4/svr4_filio.h>
50253081Sae#include <compat/svr4/svr4_sockio.h>
51253081Sae
52253081Sae#ifdef DEBUG_SVR4
53253081Saestatic void svr4_decode_cmd __P((u_long, char *, char *, int *, int *));
54253081Sae/*
55253081Sae * Decode an ioctl command symbolically
56253081Sae */
57253081Saestatic void
58253081Saesvr4_decode_cmd(cmd, dir, c, num, argsiz)
59253081Sae	u_long		  cmd;
60253081Sae	char		 *dir, *c;
61253081Sae	int		 *num, *argsiz;
62253081Sae{
63253081Sae	if (cmd & SVR4_IOC_VOID)
64105197Ssam		*dir++ = 'V';
65105197Ssam	if (cmd & SVR4_IOC_IN)
66105197Ssam		*dir++ = 'R';
67195699Srwatson	if (cmd & SVR4_IOC_OUT)
68207369Sbz		*dir++ = 'W';
69207369Sbz	*dir = '\0';
70252028Sae	if (cmd & SVR4_IOC_INOUT)
71252028Sae		*argsiz = (cmd >> 16) & 0xff;
72195727Srwatson	else
73195727Srwatson		*argsiz = -1;
74105197Ssam
75105197Ssam	*c = (cmd >> 8) & 0xff;
76	*num = cmd & 0xff;
77}
78#endif
79
80int
81svr4_sys_ioctl(td, uap)
82	register struct thread *td;
83	struct svr4_sys_ioctl_args *uap;
84{
85	int             *retval;
86	struct file	*fp;
87	u_long		 cmd;
88	int (*fun) __P((struct file *, struct thread *, register_t *,
89			int, u_long, caddr_t));
90	int error;
91#ifdef DEBUG_SVR4
92	char		 dir[4];
93	char		 c;
94	int		 num;
95	int		 argsiz;
96
97	svr4_decode_cmd(SCARG(uap, com), dir, &c, &num, &argsiz);
98
99	DPRINTF(("svr4_ioctl[%lx](%d, _IO%s(%c, %d, %d), %p);\n", SCARG(uap, com), SCARG(uap, fd),
100	    dir, c, num, argsiz, SCARG(uap, data)));
101#endif
102	retval = td->td_retval;
103	cmd = SCARG(uap, com);
104
105	if ((error = fget(td, uap->fd, &fp)) != 0)
106		return (error);
107
108	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
109		fdrop(fp, td);
110		return EBADF;
111	}
112
113#if defined(DEBUG_SVR4)
114	if (fp->f_type == DTYPE_SOCKET) {
115	        struct socket *so = (struct socket *)fp->f_data;
116		DPRINTF(("<<< IN: so_state = 0x%x\n", so->so_state));
117	}
118#endif
119
120	switch (cmd & 0xff00) {
121	case SVR4_tIOC:
122	        DPRINTF(("ttold\n"));
123		fun = svr4_ttold_ioctl;
124		break;
125
126	case SVR4_TIOC:
127	        DPRINTF(("term\n"));
128		fun = svr4_term_ioctl;
129		break;
130
131	case SVR4_STR:
132	        DPRINTF(("stream\n"));
133		fun = svr4_stream_ioctl;
134		break;
135
136	case SVR4_FIOC:
137                DPRINTF(("file\n"));
138		fun = svr4_fil_ioctl;
139		break;
140
141	case SVR4_SIOC:
142	        DPRINTF(("socket\n"));
143		fun = svr4_sock_ioctl;
144		break;
145
146	case SVR4_XIOC:
147		/* We do not support those */
148		fdrop(fp, td);
149		return EINVAL;
150
151	default:
152		fdrop(fp, td);
153		DPRINTF(("Unimplemented ioctl %lx\n", cmd));
154		return 0;	/* XXX: really ENOSYS */
155	}
156#if defined(DEBUG_SVR4)
157	if (fp->f_type == DTYPE_SOCKET) {
158	        struct socket *so;
159
160	        so = (struct socket *)fp->f_data;
161		DPRINTF((">>> OUT: so_state = 0x%x\n", so->so_state));
162	}
163#endif
164	error = (*fun)(fp, td, retval, SCARG(uap, fd), cmd, SCARG(uap, data));
165	fdrop(fp, td);
166	return (error);
167}
168