]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/svr4/svr4_filio.c
Document SA-19:09, SA-19:11.
[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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 #include <sys/capsicum.h>
36 #include <sys/file.h>
37 #include <sys/filio.h>
38 #include <sys/lock.h>
39 #include <sys/signal.h>
40 #include <sys/filedesc.h>
41 #include <sys/poll.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44
45 #include <sys/sysproto.h>
46 #include <sys/syscallsubr.h>
47
48 #include <compat/svr4/svr4.h>
49 #include <compat/svr4/svr4_types.h>
50 #include <compat/svr4/svr4_util.h>
51 #include <compat/svr4/svr4_signal.h>
52 #include <compat/svr4/svr4_proto.h>
53 #include <compat/svr4/svr4_ioctl.h>
54 #include <compat/svr4/svr4_filio.h>
55
56 /*#define GROTTY_READ_HACK*/
57
58 int
59 svr4_sys_poll(td, uap)
60      struct thread *td;
61      struct svr4_sys_poll_args *uap;
62 {
63      int error;
64      struct poll_args pa;
65      struct pollfd *pfd;
66      int idx = 0, cerr;
67      u_long siz;
68
69      if (uap->nfds > maxfilesperproc && uap->nfds > FD_SETSIZE)
70        return (EINVAL);
71
72      pa.fds = uap->fds;
73      pa.nfds = uap->nfds;
74      pa.timeout = uap->timeout;
75
76      siz = uap->nfds * sizeof(struct pollfd);
77      pfd = (struct pollfd *)malloc(siz, M_TEMP, M_WAITOK);
78
79      error = sys_poll(td, (struct poll_args *)uap);
80
81      if ((cerr = copyin(uap->fds, pfd, siz)) != 0) {
82        error = cerr;
83        goto done;
84      }
85
86      for (idx = 0; idx < uap->nfds; idx++) {
87        /* POLLWRNORM already equals POLLOUT, so we don't worry about that */
88        if (pfd[idx].revents & (POLLOUT | POLLWRNORM | POLLWRBAND))
89             pfd[idx].revents |= (POLLOUT | POLLWRNORM | POLLWRBAND);
90      }
91      if ((cerr = copyout(pfd, uap->fds, siz)) != 0) {
92        error = cerr;
93        goto done;   /* yeah, I know it's the next line, but this way I won't
94                        forget to update it if I add more code */
95      }
96 done:
97      free(pfd, M_TEMP);
98      return error;
99 }
100
101 #if defined(READ_TEST)
102 int
103 svr4_sys_read(td, uap)
104      struct thread *td;
105      struct svr4_sys_read_args *uap;
106 {
107      struct read_args ra;
108      cap_rights_t rights;
109      struct file *fp;
110      struct socket *so = NULL;
111      int so_state;
112      sigset_t sigmask;
113      int rv;
114
115      ra.fd = uap->fd;
116      ra.buf = uap->buf;
117      ra.nbyte = uap->nbyte;
118
119      if (fget(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp) != 0) {
120        DPRINTF(("Something fishy with the user-supplied file descriptor...\n"));
121        return EBADF;
122      }
123
124      if (fp->f_type == DTYPE_SOCKET) {
125        so = fp->f_data;
126        DPRINTF(("fd %d is a socket\n", uap->fd));
127        if (so->so_state & SS_ASYNC) {
128          DPRINTF(("fd %d is an ASYNC socket!\n", uap->fd));
129        }
130        DPRINTF(("Here are its flags: 0x%x\n", so->so_state));
131 #if defined(GROTTY_READ_HACK)
132        so_state = so->so_state;
133        so->so_state &= ~SS_NBIO;
134 #endif
135      }
136
137      rv = read(td, &ra);
138
139      DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n", 
140              uap->fd, uap->buf, uap->nbyte, rv));
141      if (rv == EAGAIN) {
142 #ifdef DEBUG_SVR4
143        struct sigacts *ps;
144
145        PROC_LOCK(td->td_proc);
146        ps = td->td_proc->p_sigacts;
147        mtx_lock(&ps->ps_mtx);
148 #endif
149        DPRINTF(("sigmask = 0x%x\n", td->td_sigmask));
150        DPRINTF(("sigignore = 0x%x\n", ps->ps_sigignore));
151        DPRINTF(("sigcaught = 0x%x\n", ps->ps_sigcatch));
152        DPRINTF(("siglist = 0x%x\n", td->td_siglist));
153 #ifdef DEBUG_SVR4
154        mtx_unlock(&ps->ps_mtx);
155        PROC_UNLOCK(td->td_proc);
156 #endif
157      }
158
159 #if defined(GROTTY_READ_HACK)
160      if (so) {  /* We've already checked to see if this is a socket */
161        so->so_state = so_state;
162      }
163 #endif
164      fdrop(fp, td);
165
166      return(rv);
167 }
168 #endif /* READ_TEST */
169
170 #if defined(BOGUS)
171 int
172 svr4_sys_write(td, uap)
173      struct thread *td;
174      struct svr4_sys_write_args *uap;
175 {
176      struct write_args wa;
177      struct file *fp;
178      int rv;
179
180      wa.fd = uap->fd;
181      wa.buf = uap->buf;
182      wa.nbyte = uap->nbyte;
183
184      rv = write(td, &wa);
185
186      DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n", 
187              uap->fd, uap->buf, uap->nbyte, rv));
188
189      return(rv);
190 }
191 #endif /* BOGUS */
192
193 int
194 svr4_fil_ioctl(fp, td, retval, fd, cmd, data)
195         struct file *fp;
196         struct thread *td;
197         register_t *retval;
198         int fd;
199         u_long cmd;
200         caddr_t data;
201 {
202         struct filedesc *fdp = td->td_proc->p_fd;
203         struct filedescent *fde;
204         int error, num;
205
206         *retval = 0;
207
208         switch (cmd) {
209         case SVR4_FIOCLEX:
210                 FILEDESC_XLOCK(fdp);
211                 fde = &fdp->fd_ofiles[fd];
212                 fde->fde_flags |= UF_EXCLOSE;
213                 FILEDESC_XUNLOCK(fdp);
214                 return 0;
215
216         case SVR4_FIONCLEX:
217                 FILEDESC_XLOCK(fdp);
218                 fde = &fdp->fd_ofiles[fd];
219                 fde->fde_flags &= ~UF_EXCLOSE;
220                 FILEDESC_XUNLOCK(fdp);
221                 return 0;
222
223         case SVR4_FIOGETOWN:
224         case SVR4_FIOSETOWN:
225         case SVR4_FIOASYNC:
226         case SVR4_FIONBIO:
227         case SVR4_FIONREAD:
228                 if ((error = copyin(data, &num, sizeof(num))) != 0)
229                         return error;
230
231                 switch (cmd) {
232                 case SVR4_FIOGETOWN:    cmd = FIOGETOWN; break;
233                 case SVR4_FIOSETOWN:    cmd = FIOSETOWN; break;
234                 case SVR4_FIOASYNC:     cmd = FIOASYNC;  break;
235                 case SVR4_FIONBIO:      cmd = FIONBIO;   break;
236                 case SVR4_FIONREAD:     cmd = FIONREAD;  break;
237                 }
238
239 #ifdef SVR4_DEBUG
240                 if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n"));
241 #endif
242                 error = fo_ioctl(fp, cmd, (caddr_t) &num, td->td_ucred, td);
243
244                 if (error)
245                         return error;
246
247                 return copyout(&num, data, sizeof(num));
248
249         default:
250                 DPRINTF(("Unknown svr4 filio %lx\n", cmd));
251                 return 0;       /* ENOSYS really */
252         }
253 }
254
255 int
256 svr4_pipe(struct thread *td, struct svr4_pipe_args *uap) {
257         int error;
258         int fildes[2];
259
260         error = kern_pipe(td, fildes, 0, NULL, NULL);
261         if (error)
262         return (error);
263
264         td->td_retval[0] = fildes[0];
265         td->td_retval[1] = fildes[1];
266
267         return (0);
268 }
269