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