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