svr4_filio.c revision 104306
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 104306 2002-10-01 17:15:53Z jmallett $
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       DPRINTF(("fd %d is a socket\n", SCARG(uap, fd)));
120       if (so->so_state & SS_ASYNC) {
121	 DPRINTF(("fd %d is an ASYNC socket!\n", SCARG(uap, fd)));
122       }
123       DPRINTF(("Here are its flags: 0x%x\n", so->so_state));
124#if defined(GROTTY_READ_HACK)
125       so_state = so->so_state;
126       so->so_state &= ~SS_NBIO;
127#endif
128     }
129
130     rv = read(td, &ra);
131
132     DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n",
133	     SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv));
134     if (rv == EAGAIN) {
135       DPRINTF(("sigmask = 0x%x\n", td->td_proc->p_sigmask));
136       DPRINTF(("sigignore = 0x%x\n", td->td_proc->p_sigignore));
137       DPRINTF(("sigcaught = 0x%x\n", td->td_proc->p_sigcatch));
138       DPRINTF(("siglist = 0x%x\n", td->td_proc->p_siglist));
139     }
140
141#if defined(GROTTY_READ_HACK)
142     if (so) {  /* We've already checked to see if this is a socket */
143       so->so_state = so_state;
144     }
145#endif
146     fdrop(fp, td);
147
148     return(rv);
149}
150#endif /* READ_TEST */
151
152#if defined(BOGUS)
153int
154svr4_sys_write(td, uap)
155     struct thread *td;
156     struct svr4_sys_write_args *uap;
157{
158     struct write_args wa;
159     struct file *fp;
160     int rv;
161
162     SCARG(&wa, fd) = SCARG(uap, fd);
163     SCARG(&wa, buf) = SCARG(uap, buf);
164     SCARG(&wa, nbyte) = SCARG(uap, nbyte);
165
166     rv = write(td, &wa);
167
168     DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n",
169	     SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv));
170
171     return(rv);
172}
173#endif /* BOGUS */
174
175int
176svr4_fil_ioctl(fp, td, retval, fd, cmd, data)
177	struct file *fp;
178	struct thread *td;
179	register_t *retval;
180	int fd;
181	u_long cmd;
182	caddr_t data;
183{
184	int error;
185	int num;
186	struct filedesc *fdp = td->td_proc->p_fd;
187
188	*retval = 0;
189
190	FILEDESC_LOCK(fdp);
191	switch (cmd) {
192	case SVR4_FIOCLEX:
193		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
194		FILEDESC_UNLOCK(fdp);
195		return 0;
196
197	case SVR4_FIONCLEX:
198		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
199		FILEDESC_UNLOCK(fdp);
200		return 0;
201
202	case SVR4_FIOGETOWN:
203	case SVR4_FIOSETOWN:
204	case SVR4_FIOASYNC:
205	case SVR4_FIONBIO:
206	case SVR4_FIONREAD:
207		FILEDESC_UNLOCK(fdp);
208		if ((error = copyin(data, &num, sizeof(num))) != 0)
209			return error;
210
211		switch (cmd) {
212		case SVR4_FIOGETOWN:	cmd = FIOGETOWN; break;
213		case SVR4_FIOSETOWN:	cmd = FIOSETOWN; break;
214		case SVR4_FIOASYNC:	cmd = FIOASYNC;  break;
215		case SVR4_FIONBIO:	cmd = FIONBIO;   break;
216		case SVR4_FIONREAD:	cmd = FIONREAD;  break;
217		}
218
219#ifdef SVR4_DEBUG
220		if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n"));
221#endif
222		error = fo_ioctl(fp, cmd, (caddr_t) &num, td->td_ucred, td);
223
224		if (error)
225			return error;
226
227		return copyout(&num, data, sizeof(num));
228
229	default:
230		FILEDESC_UNLOCK(fdp);
231		DPRINTF(("Unknown svr4 filio %lx\n", cmd));
232		return 0;	/* ENOSYS really */
233	}
234}
235