1139743Simp/*-
243412Snewton * Copyright (c) 1998 Mark Newton
343412Snewton * Copyright (c) 1994 Christos Zoulas
443412Snewton * All rights reserved.
543412Snewton *
643412Snewton * Redistribution and use in source and binary forms, with or without
743412Snewton * modification, are permitted provided that the following conditions
843412Snewton * are met:
943412Snewton * 1. Redistributions of source code must retain the above copyright
1043412Snewton *    notice, this list of conditions and the following disclaimer.
1143412Snewton * 2. Redistributions in binary form must reproduce the above copyright
1243412Snewton *    notice, this list of conditions and the following disclaimer in the
1343412Snewton *    documentation and/or other materials provided with the distribution.
1443412Snewton * 3. The name of the author may not be used to endorse or promote products
1543412Snewton *    derived from this software without specific prior written permission
1643412Snewton *
1743412Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1843412Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1943412Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2043412Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2143412Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2243412Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2343412Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2443412Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2543412Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2643412Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2743412Snewton */
2843412Snewton
29116174Sobrien#include <sys/cdefs.h>
30116174Sobrien__FBSDID("$FreeBSD$");
31116174Sobrien
3243412Snewton#include <sys/param.h>
3397658Stanimura#include <sys/proc.h>
34224778Srwatson#include <sys/capability.h>
3543412Snewton#include <sys/file.h>
3643412Snewton#include <sys/filedesc.h>
3797658Stanimura#include <sys/fcntl.h>
3843412Snewton#include <sys/socket.h>
3954300Snewton#include <sys/socketvar.h>
4054300Snewton#include <sys/systm.h>
4143412Snewton
4265302Sobrien#include <compat/svr4/svr4.h>
4365302Sobrien#include <compat/svr4/svr4_types.h>
4465302Sobrien#include <compat/svr4/svr4_util.h>
4565302Sobrien#include <compat/svr4/svr4_signal.h>
4665302Sobrien#include <compat/svr4/svr4_proto.h>
4765302Sobrien#include <compat/svr4/svr4_stropts.h>
4865302Sobrien#include <compat/svr4/svr4_ioctl.h>
4965302Sobrien#include <compat/svr4/svr4_termios.h>
5065302Sobrien#include <compat/svr4/svr4_filio.h>
5165302Sobrien#include <compat/svr4/svr4_sockio.h>
5243412Snewton
5343412Snewton#ifdef DEBUG_SVR4
5492761Salfredstatic void svr4_decode_cmd(u_long, char *, char *, int *, int *);
5543412Snewton/*
5643412Snewton * Decode an ioctl command symbolically
5743412Snewton */
5843412Snewtonstatic void
5943412Snewtonsvr4_decode_cmd(cmd, dir, c, num, argsiz)
6043412Snewton	u_long		  cmd;
6143412Snewton	char		 *dir, *c;
6243412Snewton	int		 *num, *argsiz;
6343412Snewton{
6443412Snewton	if (cmd & SVR4_IOC_VOID)
6543412Snewton		*dir++ = 'V';
6643412Snewton	if (cmd & SVR4_IOC_IN)
6743412Snewton		*dir++ = 'R';
6843412Snewton	if (cmd & SVR4_IOC_OUT)
6943412Snewton		*dir++ = 'W';
7043412Snewton	*dir = '\0';
7143412Snewton	if (cmd & SVR4_IOC_INOUT)
7243412Snewton		*argsiz = (cmd >> 16) & 0xff;
7343412Snewton	else
7443412Snewton		*argsiz = -1;
7543412Snewton
7643412Snewton	*c = (cmd >> 8) & 0xff;
7743412Snewton	*num = cmd & 0xff;
7843412Snewton}
7943412Snewton#endif
8043412Snewton
8143412Snewtonint
8283366Sjuliansvr4_sys_ioctl(td, uap)
83193014Sdelphij	struct thread *td;
8443412Snewton	struct svr4_sys_ioctl_args *uap;
8543412Snewton{
8643412Snewton	int             *retval;
87255219Spjd	cap_rights_t	 rights;
8843412Snewton	struct file	*fp;
8943412Snewton	u_long		 cmd;
9092761Salfred	int (*fun)(struct file *, struct thread *, register_t *,
9192761Salfred			int, u_long, caddr_t);
9289306Salfred	int error;
9343412Snewton#ifdef DEBUG_SVR4
9443412Snewton	char		 dir[4];
9543412Snewton	char		 c;
9643412Snewton	int		 num;
9743412Snewton	int		 argsiz;
9843412Snewton
99107849Salfred	svr4_decode_cmd(uap->com, dir, &c, &num, &argsiz);
10043412Snewton
101107849Salfred	DPRINTF(("svr4_ioctl[%lx](%d, _IO%s(%c, %d, %d), %p);\n", uap->com, uap->fd,
102107849Salfred	    dir, c, num, argsiz, uap->data));
10343412Snewton#endif
10483366Sjulian	retval = td->td_retval;
105107849Salfred	cmd = uap->com;
10643412Snewton
107255219Spjd	error = fget(td, uap->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
108255219Spjd	if (error != 0)
10989319Salfred		return (error);
11043412Snewton
11189306Salfred	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
11289306Salfred		fdrop(fp, td);
11343412Snewton		return EBADF;
11489306Salfred	}
11543412Snewton
11643412Snewton#if defined(DEBUG_SVR4)
11743412Snewton	if (fp->f_type == DTYPE_SOCKET) {
118109153Sdillon	        struct socket *so = fp->f_data;
11997658Stanimura		DPRINTF(("<<< IN: so_state = 0x%x\n", so->so_state));
12043412Snewton	}
12143412Snewton#endif
12243412Snewton
12343412Snewton	switch (cmd & 0xff00) {
12443412Snewton	case SVR4_TIOC:
12543412Snewton	        DPRINTF(("term\n"));
12643412Snewton		fun = svr4_term_ioctl;
12743412Snewton		break;
12843412Snewton
12943412Snewton	case SVR4_STR:
13043412Snewton	        DPRINTF(("stream\n"));
13143412Snewton		fun = svr4_stream_ioctl;
13243412Snewton		break;
13343412Snewton
13443412Snewton	case SVR4_FIOC:
13543412Snewton                DPRINTF(("file\n"));
13643412Snewton		fun = svr4_fil_ioctl;
13743412Snewton		break;
13843412Snewton
13943412Snewton	case SVR4_SIOC:
14043412Snewton	        DPRINTF(("socket\n"));
14143412Snewton		fun = svr4_sock_ioctl;
14243412Snewton		break;
14343412Snewton
14443412Snewton	case SVR4_XIOC:
14543412Snewton		/* We do not support those */
14689306Salfred		fdrop(fp, td);
14743412Snewton		return EINVAL;
14843412Snewton
14943412Snewton	default:
15089306Salfred		fdrop(fp, td);
15143412Snewton		DPRINTF(("Unimplemented ioctl %lx\n", cmd));
15243412Snewton		return 0;	/* XXX: really ENOSYS */
15343412Snewton	}
15443412Snewton#if defined(DEBUG_SVR4)
15543412Snewton	if (fp->f_type == DTYPE_SOCKET) {
15689306Salfred	        struct socket *so;
15789306Salfred
158109153Sdillon	        so = fp->f_data;
15997658Stanimura		DPRINTF((">>> OUT: so_state = 0x%x\n", so->so_state));
16043412Snewton	}
16143412Snewton#endif
162107849Salfred	error = (*fun)(fp, td, retval, uap->fd, cmd, uap->data);
16389306Salfred	fdrop(fp, td);
16489306Salfred	return (error);
16543412Snewton}
166