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