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