svr4_filio.c revision 65302
143412Snewton/* 243412Snewton * Copyright (c) 1998 Mark Newton 343412Snewton * Copyright (c) 1994 Christos Zoulas 443412Snewton * All rights reserved. 543412Snewton * 643412Snewton * Redistribution and use in source and binary forms, with or without 743412Snewton * modification, are permitted provided that the following conditions 843412Snewton * are met: 943412Snewton * 1. Redistributions of source code must retain the above copyright 1043412Snewton * notice, this list of conditions and the following disclaimer. 1143412Snewton * 2. Redistributions in binary form must reproduce the above copyright 1243412Snewton * notice, this list of conditions and the following disclaimer in the 1343412Snewton * documentation and/or other materials provided with the distribution. 1443412Snewton * 3. The name of the author may not be used to endorse or promote products 1543412Snewton * derived from this software without specific prior written permission 1643412Snewton * 1743412Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1843412Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1943412Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2043412Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2143412Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2243412Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2343412Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2443412Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2543412Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2643412Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2749267Snewton * 2850477Speter * $FreeBSD: head/sys/compat/svr4/svr4_filio.c 65302 2000-08-31 22:54:09Z obrien $ 2943412Snewton */ 3043412Snewton 3143412Snewton#include <sys/param.h> 3243412Snewton#include <sys/proc.h> 3343412Snewton#include <sys/systm.h> 3443412Snewton#include <sys/file.h> 3543412Snewton#include <sys/filio.h> 3643412Snewton#include <sys/signal.h> 3743412Snewton#include <sys/filedesc.h> 3843412Snewton#include <sys/poll.h> 3943412Snewton#include <sys/malloc.h> 4043412Snewton 4143412Snewton#include <sys/sysproto.h> 4243412Snewton 4365302Sobrien#include <compat/svr4/svr4.h> 4465302Sobrien#include <compat/svr4/svr4_types.h> 4565302Sobrien#include <compat/svr4/svr4_util.h> 4665302Sobrien#include <compat/svr4/svr4_signal.h> 4765302Sobrien#include <compat/svr4/svr4_proto.h> 4865302Sobrien#include <compat/svr4/svr4_ioctl.h> 4965302Sobrien#include <compat/svr4/svr4_filio.h> 5043412Snewton 5143412Snewton/*#define GROTTY_READ_HACK*/ 5243412Snewton 5343412Snewtonint 5443412Snewtonsvr4_sys_poll(p, uap) 5543412Snewton struct proc *p; 5643412Snewton struct svr4_sys_poll_args *uap; 5743412Snewton{ 5843412Snewton int error; 5943412Snewton struct poll_args pa; 6043412Snewton struct pollfd *pfd; 6143412Snewton int idx = 0, cerr; 6243412Snewton u_long siz; 6343412Snewton 6443412Snewton SCARG(&pa, fds) = SCARG(uap, fds); 6543412Snewton SCARG(&pa, nfds) = SCARG(uap, nfds); 6643412Snewton SCARG(&pa, timeout) = SCARG(uap, timeout); 6743412Snewton 6843412Snewton siz = SCARG(uap, nfds) * sizeof(struct pollfd); 6943412Snewton pfd = (struct pollfd *)malloc(siz, M_TEMP, M_WAITOK); 7043412Snewton 7143412Snewton error = poll(p, (struct poll_args *)uap); 7243412Snewton 7343412Snewton if ((cerr = copyin(SCARG(uap, fds), pfd, siz)) != 0) { 7443412Snewton error = cerr; 7543412Snewton goto done; 7643412Snewton } 7743412Snewton 7843412Snewton for (idx = 0; idx < SCARG(uap, nfds); idx++) { 7943412Snewton /* POLLWRNORM already equals POLLOUT, so we don't worry about that */ 8043412Snewton if (pfd[idx].revents & (POLLOUT | POLLWRNORM | POLLWRBAND)) 8143412Snewton pfd[idx].revents |= (POLLOUT | POLLWRNORM | POLLWRBAND); 8243412Snewton } 8343412Snewton if ((cerr = copyout(pfd, SCARG(uap, fds), siz)) != 0) { 8443412Snewton error = cerr; 8543412Snewton goto done; /* yeah, I know it's the next line, but this way I won't 8643412Snewton forget to update it if I add more code */ 8743412Snewton } 8843412Snewtondone: 8943412Snewton free(pfd, M_TEMP); 9043412Snewton return error; 9143412Snewton} 9243412Snewton 9343412Snewton#if defined(READ_TEST) 9443412Snewtonint 9543412Snewtonsvr4_sys_read(p, uap) 9643412Snewton struct proc *p; 9743412Snewton struct svr4_sys_read_args *uap; 9843412Snewton{ 9943412Snewton struct read_args ra; 10043412Snewton struct filedesc *fdp = p->p_fd; 10143412Snewton struct file *fp; 10243412Snewton struct socket *so = NULL; 10343412Snewton int so_state; 10443412Snewton sigset_t sigmask; 10543412Snewton int rv; 10643412Snewton 10743412Snewton SCARG(&ra, fd) = SCARG(uap, fd); 10843412Snewton SCARG(&ra, buf) = SCARG(uap, buf); 10943412Snewton SCARG(&ra, nbyte) = SCARG(uap, nbyte); 11043412Snewton 11143412Snewton if ((fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL) { 11243412Snewton DPRINTF(("Something fishy with the user-supplied file descriptor...\n")); 11343412Snewton return EBADF; 11443412Snewton } 11543412Snewton 11643412Snewton if (fp->f_type == DTYPE_SOCKET) { 11743412Snewton so = (struct socket *)fp->f_data; 11843412Snewton DPRINTF(("fd %d is a socket\n", SCARG(uap, fd))); 11943412Snewton if (so->so_state & SS_ASYNC) { 12043412Snewton DPRINTF(("fd %d is an ASYNC socket!\n", SCARG(uap, fd))); 12143412Snewton } 12243412Snewton DPRINTF(("Here are its flags: 0x%x\n", so->so_state)); 12343412Snewton#if defined(GROTTY_READ_HACK) 12443412Snewton so_state = so->so_state; 12543412Snewton so->so_state &= ~SS_NBIO; 12643412Snewton#endif 12743412Snewton } 12843412Snewton 12943412Snewton rv = read(p, &ra); 13043412Snewton 13143412Snewton DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n", 13243412Snewton SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv)); 13343412Snewton if (rv == EAGAIN) { 13443412Snewton DPRINTF(("sigmask = 0x%x\n", p->p_sigmask)); 13543412Snewton DPRINTF(("sigignore = 0x%x\n", p->p_sigignore)); 13643412Snewton DPRINTF(("sigcaught = 0x%x\n", p->p_sigcatch)); 13743412Snewton DPRINTF(("siglist = 0x%x\n", p->p_siglist)); 13843412Snewton } 13943412Snewton 14043412Snewton#if defined(GROTTY_READ_HACK) 14143412Snewton if (so) { /* We've already checked to see if this is a socket */ 14243412Snewton so->so_state = so_state; 14343412Snewton } 14443412Snewton#endif 14543412Snewton 14643412Snewton return(rv); 14743412Snewton} 14843412Snewton#endif /* READ_TEST */ 14943412Snewton 15043412Snewton#if defined(BOGUS) 15143412Snewtonint 15243412Snewtonsvr4_sys_write(p, uap) 15343412Snewton struct proc *p; 15443412Snewton struct svr4_sys_write_args *uap; 15543412Snewton{ 15643412Snewton struct write_args wa; 15743412Snewton struct filedesc *fdp; 15843412Snewton struct file *fp; 15943412Snewton int rv; 16043412Snewton 16143412Snewton SCARG(&wa, fd) = SCARG(uap, fd); 16243412Snewton SCARG(&wa, buf) = SCARG(uap, buf); 16343412Snewton SCARG(&wa, nbyte) = SCARG(uap, nbyte); 16443412Snewton 16543412Snewton rv = write(p, &wa); 16643412Snewton 16743412Snewton DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n", 16843412Snewton SCARG(uap, fd), SCARG(uap, buf), SCARG(uap, nbyte), rv)); 16943412Snewton 17043412Snewton return(rv); 17143412Snewton} 17243412Snewton#endif /* BOGUS */ 17343412Snewton 17443412Snewtonint 17543412Snewtonsvr4_fil_ioctl(fp, p, retval, fd, cmd, data) 17643412Snewton struct file *fp; 17743412Snewton struct proc *p; 17843412Snewton register_t *retval; 17943412Snewton int fd; 18043412Snewton u_long cmd; 18143412Snewton caddr_t data; 18243412Snewton{ 18343412Snewton int error; 18443412Snewton int num; 18543412Snewton struct filedesc *fdp = p->p_fd; 18643412Snewton 18743412Snewton *retval = 0; 18843412Snewton 18943412Snewton switch (cmd) { 19043412Snewton case SVR4_FIOCLEX: 19143412Snewton fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 19243412Snewton return 0; 19343412Snewton 19443412Snewton case SVR4_FIONCLEX: 19543412Snewton fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE; 19643412Snewton return 0; 19743412Snewton 19843412Snewton case SVR4_FIOGETOWN: 19943412Snewton case SVR4_FIOSETOWN: 20043412Snewton case SVR4_FIOASYNC: 20143412Snewton case SVR4_FIONBIO: 20243412Snewton case SVR4_FIONREAD: 20343412Snewton if ((error = copyin(data, &num, sizeof(num))) != 0) 20443412Snewton return error; 20543412Snewton 20643412Snewton switch (cmd) { 20743412Snewton case SVR4_FIOGETOWN: cmd = FIOGETOWN; break; 20843412Snewton case SVR4_FIOSETOWN: cmd = FIOSETOWN; break; 20943412Snewton case SVR4_FIOASYNC: cmd = FIOASYNC; break; 21043412Snewton case SVR4_FIONBIO: cmd = FIONBIO; break; 21143412Snewton case SVR4_FIONREAD: cmd = FIONREAD; break; 21243412Snewton } 21343412Snewton 21443412Snewton#ifdef SVR4_DEBUG 21543412Snewton if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n")); 21643412Snewton#endif 21751418Sgreen error = fo_ioctl(fp, cmd, (caddr_t) &num, p); 21843412Snewton 21943412Snewton if (error) 22043412Snewton return error; 22143412Snewton 22243412Snewton return copyout(&num, data, sizeof(num)); 22343412Snewton 22443412Snewton default: 22543412Snewton DPRINTF(("Unknown svr4 filio %lx\n", cmd)); 22643412Snewton return 0; /* ENOSYS really */ 22743412Snewton } 22843412Snewton} 229