svr4_filio.c revision 51418
1/*
2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/compat/svr4/svr4_filio.c 51418 1999-09-19 17:00:25Z green $
29 */
30
31#include <sys/param.h>
32#include <sys/proc.h>
33#include <sys/systm.h>
34#include <sys/file.h>
35#include <sys/filio.h>
36#include <sys/fcntl.h>
37#include <sys/signal.h>
38#include <sys/unistd.h>
39#include <sys/filedesc.h>
40#include <sys/termios.h>
41#include <sys/tty.h>
42#include <sys/poll.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/mount.h>
46#include <net/if.h>
47#include <sys/malloc.h>
48
49#include <sys/sysproto.h>
50
51#include <svr4/svr4.h>
52#include <svr4/svr4_types.h>
53#include <svr4/svr4_util.h>
54#include <svr4/svr4_signal.h>
55#include <svr4/svr4_proto.h>
56#include <svr4/svr4_ioctl.h>
57#include <svr4/svr4_filio.h>
58
59/*#define GROTTY_READ_HACK*/
60
61int
62svr4_sys_poll(p, uap)
63     struct proc *p;
64     struct svr4_sys_poll_args *uap;
65{
66     int error;
67     struct poll_args pa;
68     struct pollfd *pfd;
69     int idx = 0, cerr;
70     u_long siz;
71
72     SCARG(&pa, fds) = SCARG(uap, fds);
73     SCARG(&pa, nfds) = SCARG(uap, nfds);
74     SCARG(&pa, timeout) = SCARG(uap, timeout);
75
76     siz = SCARG(uap, nfds) * sizeof(struct pollfd);
77     pfd = (struct pollfd *)malloc(siz, M_TEMP, M_WAITOK);
78
79     error = poll(p, (struct poll_args *)uap);
80
81     if ((cerr = copyin(SCARG(uap, fds), pfd, siz)) != 0) {
82       error = cerr;
83       goto done;
84     }
85     DPRINTF(("poll(%x, %x, %d) = %d\n", SCARG(uap, fds), SCARG(uap, nfds),
86	      SCARG(uap, timeout), p->p_retval[0]));
87
88     for (idx = 0; idx < SCARG(uap, nfds); idx++) {
89       /* POLLWRNORM already equals POLLOUT, so we don't worry about that */
90       if (pfd[idx].revents & (POLLOUT | POLLWRNORM | POLLWRBAND))
91	    pfd[idx].revents |= (POLLOUT | POLLWRNORM | POLLWRBAND);
92       DPRINTF(("pollfd[%d]->fd = %d\n", idx, pfd[idx].fd));
93       DPRINTF(("pollfd[%d]->events = %x\n", idx, pfd[idx].events));
94       DPRINTF(("pollfd[%d]->revents = %x\n", idx, pfd[idx].revents));
95     }
96     if ((cerr = copyout(pfd, SCARG(uap, fds), siz)) != 0) {
97       error = cerr;
98       goto done;   /* yeah, I know it's the next line, but this way I won't
99		       forget to update it if I add more code */
100     }
101done:
102     free(pfd, M_TEMP);
103     return error;
104}
105
106#if defined(READ_TEST)
107int
108svr4_sys_read(p, uap)
109     struct proc *p;
110     struct svr4_sys_read_args *uap;
111{
112     struct read_args ra;
113     struct filedesc *fdp = p->p_fd;
114     struct file *fp;
115     struct socket *so = NULL;
116     int so_state;
117     sigset_t sigmask;
118     int rv;
119
120     SCARG(&ra, fd) = SCARG(uap, fd);
121     SCARG(&ra, buf) = SCARG(uap, buf);
122     SCARG(&ra, nbyte) = SCARG(uap, nbyte);
123
124     if ((fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL) {
125       DPRINTF(("Something fishy with the user-supplied file descriptor...\n"));
126       return EBADF;
127     }
128
129     if (fp->f_type == DTYPE_SOCKET) {
130       so = (struct socket *)fp->f_data;
131       DPRINTF(("fd %d is a socket\n", SCARG(uap, fd)));
132       if (so->so_state & SS_ASYNC) {
133	 DPRINTF(("fd %d is an ASYNC socket!\n", SCARG(uap, fd)));
134       }
135       DPRINTF(("Here are its flags: 0x%x\n", so->so_state));
136#if defined(GROTTY_READ_HACK)
137       so_state = so->so_state;
138       so->so_state &= ~SS_NBIO;
139#endif
140     }
141
142     rv = read(p, &ra);
143
144     DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n",
145	     SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv));
146     if (rv == EAGAIN) {
147       DPRINTF(("sigmask = 0x%x\n", p->p_sigmask));
148       DPRINTF(("sigignore = 0x%x\n", p->p_sigignore));
149       DPRINTF(("sigcaught = 0x%x\n", p->p_sigcatch));
150       DPRINTF(("siglist = 0x%x\n", p->p_siglist));
151     }
152
153#if defined(GROTTY_READ_HACK)
154     if (so) {  /* We've already checked to see if this is a socket */
155       so->so_state = so_state;
156     }
157#endif
158
159     return(rv);
160}
161#endif /* READ_TEST */
162
163#if defined(BOGUS)
164int
165svr4_sys_write(p, uap)
166     struct proc *p;
167     struct svr4_sys_write_args *uap;
168{
169     struct write_args wa;
170     struct filedesc *fdp;
171     struct file *fp;
172     int rv;
173
174     SCARG(&wa, fd) = SCARG(uap, fd);
175     SCARG(&wa, buf) = SCARG(uap, buf);
176     SCARG(&wa, nbyte) = SCARG(uap, nbyte);
177
178     rv = write(p, &wa);
179
180     DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n",
181	     SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv));
182
183     return(rv);
184}
185#endif /* BOGUS */
186
187int
188svr4_fil_ioctl(fp, p, retval, fd, cmd, data)
189	struct file *fp;
190	struct proc *p;
191	register_t *retval;
192	int fd;
193	u_long cmd;
194	caddr_t data;
195{
196	int error;
197	int num;
198	struct filedesc *fdp = p->p_fd;
199
200	*retval = 0;
201
202	switch (cmd) {
203	case SVR4_FIOCLEX:
204		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
205		return 0;
206
207	case SVR4_FIONCLEX:
208		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
209		return 0;
210
211	case SVR4_FIOGETOWN:
212	case SVR4_FIOSETOWN:
213	case SVR4_FIOASYNC:
214	case SVR4_FIONBIO:
215	case SVR4_FIONREAD:
216		if ((error = copyin(data, &num, sizeof(num))) != 0)
217			return error;
218
219		switch (cmd) {
220		case SVR4_FIOGETOWN:	cmd = FIOGETOWN; break;
221		case SVR4_FIOSETOWN:	cmd = FIOSETOWN; break;
222		case SVR4_FIOASYNC:	cmd = FIOASYNC;  break;
223		case SVR4_FIONBIO:	cmd = FIONBIO;   break;
224		case SVR4_FIONREAD:	cmd = FIONREAD;  break;
225		}
226
227#ifdef SVR4_DEBUG
228		if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n"));
229#endif
230		error = fo_ioctl(fp, cmd, (caddr_t) &num, p);
231
232		if (error)
233			return error;
234
235		return copyout(&num, data, sizeof(num));
236
237	default:
238		DPRINTF(("Unknown svr4 filio %lx\n", cmd));
239		return 0;	/* ENOSYS really */
240	}
241}
242