svr4_filio.c revision 96972
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 96972 2002-05-20 05:41:09Z tanimura $
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/lock.h>
37#include <sys/signal.h>
38#include <sys/filedesc.h>
39#include <sys/poll.h>
40#include <sys/malloc.h>
41#include <sys/mutex.h>
42
43#include <sys/sysproto.h>
44
45#include <compat/svr4/svr4.h>
46#include <compat/svr4/svr4_types.h>
47#include <compat/svr4/svr4_util.h>
48#include <compat/svr4/svr4_signal.h>
49#include <compat/svr4/svr4_proto.h>
50#include <compat/svr4/svr4_ioctl.h>
51#include <compat/svr4/svr4_filio.h>
52
53/*#define GROTTY_READ_HACK*/
54
55int
56svr4_sys_poll(td, uap)
57     struct thread *td;
58     struct svr4_sys_poll_args *uap;
59{
60     int error;
61     struct poll_args pa;
62     struct pollfd *pfd;
63     int idx = 0, cerr;
64     u_long siz;
65
66     SCARG(&pa, fds) = SCARG(uap, fds);
67     SCARG(&pa, nfds) = SCARG(uap, nfds);
68     SCARG(&pa, timeout) = SCARG(uap, timeout);
69
70     siz = SCARG(uap, nfds) * sizeof(struct pollfd);
71     pfd = (struct pollfd *)malloc(siz, M_TEMP, M_WAITOK);
72
73     error = poll(td, (struct poll_args *)uap);
74
75     if ((cerr = copyin(SCARG(uap, fds), pfd, siz)) != 0) {
76       error = cerr;
77       goto done;
78     }
79
80     for (idx = 0; idx < SCARG(uap, nfds); idx++) {
81       /* POLLWRNORM already equals POLLOUT, so we don't worry about that */
82       if (pfd[idx].revents & (POLLOUT | POLLWRNORM | POLLWRBAND))
83	    pfd[idx].revents |= (POLLOUT | POLLWRNORM | POLLWRBAND);
84     }
85     if ((cerr = copyout(pfd, SCARG(uap, fds), siz)) != 0) {
86       error = cerr;
87       goto done;   /* yeah, I know it's the next line, but this way I won't
88		       forget to update it if I add more code */
89     }
90done:
91     free(pfd, M_TEMP);
92     return error;
93}
94
95#if defined(READ_TEST)
96int
97svr4_sys_read(td, uap)
98     struct thread *td;
99     struct svr4_sys_read_args *uap;
100{
101     struct read_args ra;
102     struct file *fp;
103     struct socket *so = NULL;
104     int so_state;
105     sigset_t sigmask;
106     int rv;
107
108     SCARG(&ra, fd) = SCARG(uap, fd);
109     SCARG(&ra, buf) = SCARG(uap, buf);
110     SCARG(&ra, nbyte) = SCARG(uap, nbyte);
111
112     if (fget(td, uap->fd, &fp) != 0) {
113       DPRINTF(("Something fishy with the user-supplied file descriptor...\n"));
114       return EBADF;
115     }
116
117     if (fp->f_type == DTYPE_SOCKET) {
118       so = (struct socket *)fp->f_data;
119       SOCK_LOCK(so);
120       so_state = so->so_state;
121#if defined(GROTTY_READ_HACK)
122       so->so_state &= ~SS_NBIO;
123#endif
124       SOCK_UNLOCK(so);
125       DPRINTF(("fd %d is a socket\n", SCARG(uap, fd)));
126       if (so_state & SS_ASYNC) {
127	 DPRINTF(("fd %d is an ASYNC socket!\n", SCARG(uap, fd)));
128       }
129       DPRINTF(("Here are its flags: 0x%x\n", so_state));
130     }
131
132     rv = read(td, &ra);
133
134     DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n",
135	     SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv));
136     if (rv == EAGAIN) {
137       DPRINTF(("sigmask = 0x%x\n", td->td_proc->p_sigmask));
138       DPRINTF(("sigignore = 0x%x\n", td->td_proc->p_sigignore));
139       DPRINTF(("sigcaught = 0x%x\n", td->td_proc->p_sigcatch));
140       DPRINTF(("siglist = 0x%x\n", td->td_proc->p_siglist));
141     }
142
143#if defined(GROTTY_READ_HACK)
144     if (so) {  /* We've already checked to see if this is a socket */
145       SOCK_LOCK(so);
146       so->so_state = so_state;
147       SOCK_UNLOCK(so);
148     }
149#endif
150     fdrop(fp, td);
151
152     return(rv);
153}
154#endif /* READ_TEST */
155
156#if defined(BOGUS)
157int
158svr4_sys_write(td, uap)
159     struct thread *td;
160     struct svr4_sys_write_args *uap;
161{
162     struct write_args wa;
163     struct file *fp;
164     int rv;
165
166     SCARG(&wa, fd) = SCARG(uap, fd);
167     SCARG(&wa, buf) = SCARG(uap, buf);
168     SCARG(&wa, nbyte) = SCARG(uap, nbyte);
169
170     rv = write(td, &wa);
171
172     DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n",
173	     SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv));
174
175     return(rv);
176}
177#endif /* BOGUS */
178
179int
180svr4_fil_ioctl(fp, td, retval, fd, cmd, data)
181	struct file *fp;
182	struct thread *td;
183	register_t *retval;
184	int fd;
185	u_long cmd;
186	caddr_t data;
187{
188	int error;
189	int num;
190	struct filedesc *fdp = td->td_proc->p_fd;
191
192	*retval = 0;
193
194	FILEDESC_LOCK(fdp);
195	switch (cmd) {
196	case SVR4_FIOCLEX:
197		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
198		FILEDESC_UNLOCK(fdp);
199		return 0;
200
201	case SVR4_FIONCLEX:
202		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
203		FILEDESC_UNLOCK(fdp);
204		return 0;
205
206	case SVR4_FIOGETOWN:
207	case SVR4_FIOSETOWN:
208	case SVR4_FIOASYNC:
209	case SVR4_FIONBIO:
210	case SVR4_FIONREAD:
211		FILEDESC_UNLOCK(fdp);
212		if ((error = copyin(data, &num, sizeof(num))) != 0)
213			return error;
214
215		switch (cmd) {
216		case SVR4_FIOGETOWN:	cmd = FIOGETOWN; break;
217		case SVR4_FIOSETOWN:	cmd = FIOSETOWN; break;
218		case SVR4_FIOASYNC:	cmd = FIOASYNC;  break;
219		case SVR4_FIONBIO:	cmd = FIONBIO;   break;
220		case SVR4_FIONREAD:	cmd = FIONREAD;  break;
221		}
222
223#ifdef SVR4_DEBUG
224		if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n"));
225#endif
226		error = fo_ioctl(fp, cmd, (caddr_t) &num, td);
227
228		if (error)
229			return error;
230
231		return copyout(&num, data, sizeof(num));
232
233	default:
234		FILEDESC_UNLOCK(fdp);
235		DPRINTF(("Unknown svr4 filio %lx\n", cmd));
236		return 0;	/* ENOSYS really */
237	}
238}
239