svr4_filio.c revision 210197
1139743Simp/*-
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.
2743412Snewton */
2843412Snewton
29116174Sobrien#include <sys/cdefs.h>
30116174Sobrien__FBSDID("$FreeBSD: head/sys/compat/svr4/svr4_filio.c 210197 2010-07-17 15:52:11Z trasz $");
31116174Sobrien
3243412Snewton#include <sys/param.h>
3343412Snewton#include <sys/proc.h>
3443412Snewton#include <sys/systm.h>
3543412Snewton#include <sys/file.h>
3643412Snewton#include <sys/filio.h>
3790002Salfred#include <sys/lock.h>
3843412Snewton#include <sys/signal.h>
3943412Snewton#include <sys/filedesc.h>
4043412Snewton#include <sys/poll.h>
4143412Snewton#include <sys/malloc.h>
4290002Salfred#include <sys/mutex.h>
4343412Snewton
4443412Snewton#include <sys/sysproto.h>
4543412Snewton
4665302Sobrien#include <compat/svr4/svr4.h>
4765302Sobrien#include <compat/svr4/svr4_types.h>
4865302Sobrien#include <compat/svr4/svr4_util.h>
4965302Sobrien#include <compat/svr4/svr4_signal.h>
5065302Sobrien#include <compat/svr4/svr4_proto.h>
5165302Sobrien#include <compat/svr4/svr4_ioctl.h>
5265302Sobrien#include <compat/svr4/svr4_filio.h>
5343412Snewton
5443412Snewton/*#define GROTTY_READ_HACK*/
5543412Snewton
5643412Snewtonint
5783366Sjuliansvr4_sys_poll(td, uap)
5883366Sjulian     struct thread *td;
5943412Snewton     struct svr4_sys_poll_args *uap;
6043412Snewton{
6143412Snewton     int error;
6243412Snewton     struct poll_args pa;
6343412Snewton     struct pollfd *pfd;
6443412Snewton     int idx = 0, cerr;
6543412Snewton     u_long siz;
6643412Snewton
67210197Strasz     if (uap->nfds > maxfilesperproc && uap->nfds > FD_SETSIZE)
68125454Sjhb       return (EINVAL);
69121275Stjr
70107849Salfred     pa.fds = uap->fds;
71107849Salfred     pa.nfds = uap->nfds;
72107849Salfred     pa.timeout = uap->timeout;
7343412Snewton
74107849Salfred     siz = uap->nfds * sizeof(struct pollfd);
75111119Simp     pfd = (struct pollfd *)malloc(siz, M_TEMP, M_WAITOK);
7643412Snewton
7783366Sjulian     error = poll(td, (struct poll_args *)uap);
7843412Snewton
79107849Salfred     if ((cerr = copyin(uap->fds, pfd, siz)) != 0) {
8043412Snewton       error = cerr;
8143412Snewton       goto done;
8243412Snewton     }
8343412Snewton
84107849Salfred     for (idx = 0; idx < uap->nfds; idx++) {
8543412Snewton       /* POLLWRNORM already equals POLLOUT, so we don't worry about that */
8643412Snewton       if (pfd[idx].revents & (POLLOUT | POLLWRNORM | POLLWRBAND))
8743412Snewton	    pfd[idx].revents |= (POLLOUT | POLLWRNORM | POLLWRBAND);
8843412Snewton     }
89107849Salfred     if ((cerr = copyout(pfd, uap->fds, siz)) != 0) {
9043412Snewton       error = cerr;
9143412Snewton       goto done;   /* yeah, I know it's the next line, but this way I won't
9243412Snewton		       forget to update it if I add more code */
9343412Snewton     }
9443412Snewtondone:
9543412Snewton     free(pfd, M_TEMP);
9643412Snewton     return error;
9743412Snewton}
9843412Snewton
9943412Snewton#if defined(READ_TEST)
10043412Snewtonint
10183366Sjuliansvr4_sys_read(td, uap)
10283366Sjulian     struct thread *td;
10343412Snewton     struct svr4_sys_read_args *uap;
10443412Snewton{
10543412Snewton     struct read_args ra;
10643412Snewton     struct file *fp;
10743412Snewton     struct socket *so = NULL;
10843412Snewton     int so_state;
10943412Snewton     sigset_t sigmask;
11043412Snewton     int rv;
11143412Snewton
112107849Salfred     ra.fd = uap->fd;
113107849Salfred     ra.buf = uap->buf;
114107849Salfred     ra.nbyte = uap->nbyte;
11543412Snewton
11689319Salfred     if (fget(td, uap->fd, &fp) != 0) {
11743412Snewton       DPRINTF(("Something fishy with the user-supplied file descriptor...\n"));
11843412Snewton       return EBADF;
11943412Snewton     }
12043412Snewton
12143412Snewton     if (fp->f_type == DTYPE_SOCKET) {
122109153Sdillon       so = fp->f_data;
123107849Salfred       DPRINTF(("fd %d is a socket\n", uap->fd));
12497658Stanimura       if (so->so_state & SS_ASYNC) {
125107849Salfred	 DPRINTF(("fd %d is an ASYNC socket!\n", uap->fd));
12697658Stanimura       }
12797658Stanimura       DPRINTF(("Here are its flags: 0x%x\n", so->so_state));
12897658Stanimura#if defined(GROTTY_READ_HACK)
12996972Stanimura       so_state = so->so_state;
13096972Stanimura       so->so_state &= ~SS_NBIO;
13196972Stanimura#endif
13243412Snewton     }
13343412Snewton
13483366Sjulian     rv = read(td, &ra);
13543412Snewton
13643412Snewton     DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n",
137107849Salfred	     uap->fd, uap->buf, uap->nbyte, rv));
13843412Snewton     if (rv == EAGAIN) {
139114983Sjhb#ifdef DEBUG_SVR4
140114983Sjhb       struct sigacts *ps;
141114983Sjhb
142114983Sjhb       PROC_LOCK(td->td_proc);
143114983Sjhb       ps = td->td_proc->p_sigacts;
144114983Sjhb       mtx_lock(&ps->ps_mtx);
145114983Sjhb#endif
146112888Sjeff       DPRINTF(("sigmask = 0x%x\n", td->td_sigmask));
147114983Sjhb       DPRINTF(("sigignore = 0x%x\n", ps->ps_sigignore));
148114983Sjhb       DPRINTF(("sigcaught = 0x%x\n", ps->ps_sigcatch));
149112888Sjeff       DPRINTF(("siglist = 0x%x\n", td->td_siglist));
150114983Sjhb#ifdef DEBUG_SVR4
151114983Sjhb       mtx_unlock(&ps->ps_mtx);
152114983Sjhb       PROC_UNLOCK(td->td_proc);
153114983Sjhb#endif
15443412Snewton     }
15543412Snewton
15643412Snewton#if defined(GROTTY_READ_HACK)
15743412Snewton     if (so) {  /* We've already checked to see if this is a socket */
15843412Snewton       so->so_state = so_state;
15943412Snewton     }
16043412Snewton#endif
16189306Salfred     fdrop(fp, td);
16243412Snewton
16343412Snewton     return(rv);
16443412Snewton}
16543412Snewton#endif /* READ_TEST */
16643412Snewton
16743412Snewton#if defined(BOGUS)
16843412Snewtonint
16983366Sjuliansvr4_sys_write(td, uap)
17083366Sjulian     struct thread *td;
17143412Snewton     struct svr4_sys_write_args *uap;
17243412Snewton{
17343412Snewton     struct write_args wa;
17443412Snewton     struct file *fp;
17543412Snewton     int rv;
17643412Snewton
177107849Salfred     wa.fd = uap->fd;
178107849Salfred     wa.buf = uap->buf;
179107849Salfred     wa.nbyte = uap->nbyte;
18043412Snewton
18183366Sjulian     rv = write(td, &wa);
18243412Snewton
18343412Snewton     DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n",
184107849Salfred	     uap->fd, uap->buf, uap->nbyte, rv));
18543412Snewton
18643412Snewton     return(rv);
18743412Snewton}
18843412Snewton#endif /* BOGUS */
18943412Snewton
19043412Snewtonint
19183366Sjuliansvr4_fil_ioctl(fp, td, retval, fd, cmd, data)
19243412Snewton	struct file *fp;
19383366Sjulian	struct thread *td;
19443412Snewton	register_t *retval;
19543412Snewton	int fd;
19643412Snewton	u_long cmd;
19743412Snewton	caddr_t data;
19843412Snewton{
19943412Snewton	int error;
20043412Snewton	int num;
20183366Sjulian	struct filedesc *fdp = td->td_proc->p_fd;
20243412Snewton
20343412Snewton	*retval = 0;
20443412Snewton
20543412Snewton	switch (cmd) {
20643412Snewton	case SVR4_FIOCLEX:
207168355Srwatson		FILEDESC_XLOCK(fdp);
20843412Snewton		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
209168355Srwatson		FILEDESC_XUNLOCK(fdp);
21043412Snewton		return 0;
21143412Snewton
21243412Snewton	case SVR4_FIONCLEX:
213168355Srwatson		FILEDESC_XLOCK(fdp);
21443412Snewton		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
215168355Srwatson		FILEDESC_XUNLOCK(fdp);
21643412Snewton		return 0;
21743412Snewton
21843412Snewton	case SVR4_FIOGETOWN:
21943412Snewton	case SVR4_FIOSETOWN:
22043412Snewton	case SVR4_FIOASYNC:
22143412Snewton	case SVR4_FIONBIO:
22243412Snewton	case SVR4_FIONREAD:
22343412Snewton		if ((error = copyin(data, &num, sizeof(num))) != 0)
22443412Snewton			return error;
22543412Snewton
22643412Snewton		switch (cmd) {
22743412Snewton		case SVR4_FIOGETOWN:	cmd = FIOGETOWN; break;
22843412Snewton		case SVR4_FIOSETOWN:	cmd = FIOSETOWN; break;
22943412Snewton		case SVR4_FIOASYNC:	cmd = FIOASYNC;  break;
23043412Snewton		case SVR4_FIONBIO:	cmd = FIONBIO;   break;
23143412Snewton		case SVR4_FIONREAD:	cmd = FIONREAD;  break;
23243412Snewton		}
23343412Snewton
23443412Snewton#ifdef SVR4_DEBUG
23543412Snewton		if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n"));
23643412Snewton#endif
237102003Srwatson		error = fo_ioctl(fp, cmd, (caddr_t) &num, td->td_ucred, td);
23843412Snewton
23943412Snewton		if (error)
24043412Snewton			return error;
24143412Snewton
24243412Snewton		return copyout(&num, data, sizeof(num));
24343412Snewton
24443412Snewton	default:
24543412Snewton		DPRINTF(("Unknown svr4 filio %lx\n", cmd));
24643412Snewton		return 0;	/* ENOSYS really */
24743412Snewton	}
24843412Snewton}
249