svr4_ioctl.c revision 49267
195023Ssuz/*
262655Skris * Copyright (c) 1998 Mark Newton
356668Sshin * Copyright (c) 1994 Christos Zoulas
456668Sshin * All rights reserved.
556668Sshin *
662655Skris * Redistribution and use in source and binary forms, with or without
756668Sshin * modification, are permitted provided that the following conditions
856668Sshin * are met:
956668Sshin * 1. Redistributions of source code must retain the above copyright
1056668Sshin *    notice, this list of conditions and the following disclaimer.
1156668Sshin * 2. Redistributions in binary form must reproduce the above copyright
1256668Sshin *    notice, this list of conditions and the following disclaimer in the
1356668Sshin *    documentation and/or other materials provided with the distribution.
1456668Sshin * 3. The name of the author may not be used to endorse or promote products
1556668Sshin *    derived from this software without specific prior written permission
1656668Sshin *
1756668Sshin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1862655Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1956668Sshin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2056668Sshin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2156668Sshin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2256668Sshin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2356668Sshin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2456668Sshin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2556668Sshin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2656668Sshin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2756668Sshin *
2856668Sshin * $Id$
2956668Sshin */
3056668Sshin
3156668Sshin#include <sys/param.h>
3256668Sshin#include <sys/proc.h>
3356668Sshin#include <sys/systm.h>
3456668Sshin#include <sys/file.h>
3556668Sshin#include <sys/filedesc.h>
3656668Sshin#include <sys/unistd.h>
3756668Sshin#include <sys/fcntl.h>
3856668Sshin#include <sys/termios.h>
3956668Sshin#include <sys/tty.h>
4056668Sshin#include <sys/socket.h>
4156668Sshin#include <sys/socketvar.h>
4256668Sshin#include <sys/mount.h>
4356668Sshin#include <net/if.h>
4456668Sshin#include <sys/malloc.h>
4556668Sshin
4656668Sshin#include <sys/sysproto.h>
4756668Sshin
4856668Sshin#include <svr4/svr4.h>
4956668Sshin#include <svr4/svr4_types.h>
5056668Sshin#include <svr4/svr4_util.h>
5156668Sshin#include <svr4/svr4_signal.h>
5256668Sshin#include <svr4/svr4_proto.h>
5356668Sshin#include <svr4/svr4_stropts.h>
5456668Sshin#include <svr4/svr4_ioctl.h>
5556668Sshin#include <svr4/svr4_termios.h>
5656668Sshin#include <svr4/svr4_ttold.h>
5756668Sshin#include <svr4/svr4_filio.h>
5856668Sshin#include <svr4/svr4_sockio.h>
5956668Sshin
6056668Sshin#ifdef DEBUG_SVR4
6156668Sshinstatic void svr4_decode_cmd __P((u_long, char *, char *, int *, int *));
6256668Sshin/*
6356668Sshin * Decode an ioctl command symbolically
6456668Sshin */
6556668Sshinstatic void
6656668Sshinsvr4_decode_cmd(cmd, dir, c, num, argsiz)
6756668Sshin	u_long		  cmd;
6856668Sshin	char		 *dir, *c;
6956668Sshin	int		 *num, *argsiz;
7056668Sshin{
7156668Sshin	if (cmd & SVR4_IOC_VOID)
7256668Sshin		*dir++ = 'V';
7356668Sshin	if (cmd & SVR4_IOC_IN)
7456668Sshin		*dir++ = 'R';
7556668Sshin	if (cmd & SVR4_IOC_OUT)
7656668Sshin		*dir++ = 'W';
7756668Sshin	*dir = '\0';
7856668Sshin	if (cmd & SVR4_IOC_INOUT)
7956668Sshin		*argsiz = (cmd >> 16) & 0xff;
8056668Sshin	else
8156668Sshin		*argsiz = -1;
8256668Sshin
8356668Sshin	*c = (cmd >> 8) & 0xff;
8456668Sshin	*num = cmd & 0xff;
8556668Sshin}
8656668Sshin#endif
8756668Sshin
8856668Sshinint
8956668Sshinsvr4_sys_ioctl(p, uap)
9056668Sshin	register struct proc *p;
9156668Sshin	struct svr4_sys_ioctl_args *uap;
9256668Sshin{
9356668Sshin	int             *retval;
9456668Sshin	struct file	*fp;
9556668Sshin	struct filedesc	*fdp;
9656668Sshin	u_long		 cmd;
9756668Sshin	int (*fun) __P((struct file *, struct proc *, register_t *,
9856668Sshin			int, u_long, caddr_t));
9956668Sshin#ifdef DEBUG_SVR4
10056668Sshin	char		 dir[4];
10156668Sshin	char		 c;
10256668Sshin	int		 num;
10356668Sshin	int		 argsiz;
10456668Sshin
10556668Sshin	svr4_decode_cmd(SCARG(uap, com), dir, &c, &num, &argsiz);
10695023Ssuz
10756668Sshin	DPRINTF(("svr4_ioctl[%x](%d, _IO%s(%c, %d, %d), %p);\n", SCARG(uap, com), SCARG(uap, fd),
10856668Sshin	    dir, c, num, argsiz, SCARG(uap, data)));
10956668Sshin#endif
11056668Sshin	retval = p->p_retval;
11156668Sshin	fdp = p->p_fd;
11256668Sshin	cmd = SCARG(uap, com);
11356668Sshin
11456668Sshin	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
11556668Sshin	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
11656668Sshin		return EBADF;
11756668Sshin
11856668Sshin	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
11956668Sshin		return EBADF;
12056668Sshin
12156668Sshin#if defined(DEBUG_SVR4)
12256668Sshin	if (fp->f_type == DTYPE_SOCKET) {
12381981Sbrian	        struct socket *so = fp->f_data;
12456668Sshin		DPRINTF(("<<< IN: so_state = 0x%x\n", so->so_state));
12581981Sbrian	}
12656668Sshin#endif
12756668Sshin
12856668Sshin	switch (cmd & 0xff00) {
12956668Sshin	case SVR4_tIOC:
13056668Sshin	        DPRINTF(("ttold\n"));
13156668Sshin		fun = svr4_ttold_ioctl;
13256668Sshin		break;
13356668Sshin
13456668Sshin	case SVR4_TIOC:
13556668Sshin	        DPRINTF(("term\n"));
13656668Sshin		fun = svr4_term_ioctl;
13756668Sshin		break;
13881981Sbrian
13956668Sshin	case SVR4_STR:
14081981Sbrian	        DPRINTF(("stream\n"));
14156668Sshin		fun = svr4_stream_ioctl;
14256668Sshin		break;
14356668Sshin
14456668Sshin	case SVR4_FIOC:
14556668Sshin                DPRINTF(("file\n"));
14656668Sshin		fun = svr4_fil_ioctl;
14756668Sshin		break;
14856668Sshin
14956668Sshin	case SVR4_SIOC:
15056668Sshin	        DPRINTF(("socket\n"));
15156668Sshin		fun = svr4_sock_ioctl;
15256668Sshin		break;
15356668Sshin
15456668Sshin	case SVR4_XIOC:
15556668Sshin		/* We do not support those */
15656668Sshin		return EINVAL;
15756668Sshin
15856668Sshin	default:
15956668Sshin		DPRINTF(("Unimplemented ioctl %lx\n", cmd));
16056668Sshin		return 0;	/* XXX: really ENOSYS */
16156668Sshin	}
16256668Sshin#if defined(DEBUG_SVR4)
16356668Sshin	if (fp->f_type == DTYPE_SOCKET) {
16456668Sshin	        struct socket *so = fp->f_data;
16556668Sshin		DPRINTF((">>> OUT: so_state = 0x%x\n", so->so_state));
16656668Sshin	}
16756668Sshin#endif
16856668Sshin	return (*fun)(fp, p, retval, SCARG(uap, fd), cmd, SCARG(uap, data));
16956668Sshin}
17056668Sshin