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