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