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$");
31116174Sobrien
3243412Snewton#include <sys/param.h>
3343412Snewton#include <sys/proc.h>
3443412Snewton#include <sys/systm.h>
35224778Srwatson#include <sys/capability.h>
3643412Snewton#include <sys/file.h>
3743412Snewton#include <sys/filio.h>
3890002Salfred#include <sys/lock.h>
3943412Snewton#include <sys/signal.h>
4043412Snewton#include <sys/filedesc.h>
4143412Snewton#include <sys/poll.h>
4243412Snewton#include <sys/malloc.h>
4390002Salfred#include <sys/mutex.h>
4443412Snewton
4543412Snewton#include <sys/sysproto.h>
4643412Snewton
4765302Sobrien#include <compat/svr4/svr4.h>
4865302Sobrien#include <compat/svr4/svr4_types.h>
4965302Sobrien#include <compat/svr4/svr4_util.h>
5065302Sobrien#include <compat/svr4/svr4_signal.h>
5165302Sobrien#include <compat/svr4/svr4_proto.h>
5265302Sobrien#include <compat/svr4/svr4_ioctl.h>
5365302Sobrien#include <compat/svr4/svr4_filio.h>
5443412Snewton
5543412Snewton/*#define GROTTY_READ_HACK*/
5643412Snewton
5743412Snewtonint
5883366Sjuliansvr4_sys_poll(td, uap)
5983366Sjulian     struct thread *td;
6043412Snewton     struct svr4_sys_poll_args *uap;
6143412Snewton{
6243412Snewton     int error;
6343412Snewton     struct poll_args pa;
6443412Snewton     struct pollfd *pfd;
6543412Snewton     int idx = 0, cerr;
6643412Snewton     u_long siz;
6743412Snewton
68210197Strasz     if (uap->nfds > maxfilesperproc && uap->nfds > FD_SETSIZE)
69125454Sjhb       return (EINVAL);
70121275Stjr
71107849Salfred     pa.fds = uap->fds;
72107849Salfred     pa.nfds = uap->nfds;
73107849Salfred     pa.timeout = uap->timeout;
7443412Snewton
75107849Salfred     siz = uap->nfds * sizeof(struct pollfd);
76111119Simp     pfd = (struct pollfd *)malloc(siz, M_TEMP, M_WAITOK);
7743412Snewton
78225617Skmacy     error = sys_poll(td, (struct poll_args *)uap);
7943412Snewton
80107849Salfred     if ((cerr = copyin(uap->fds, pfd, siz)) != 0) {
8143412Snewton       error = cerr;
8243412Snewton       goto done;
8343412Snewton     }
8443412Snewton
85107849Salfred     for (idx = 0; idx < uap->nfds; idx++) {
8643412Snewton       /* POLLWRNORM already equals POLLOUT, so we don't worry about that */
8743412Snewton       if (pfd[idx].revents & (POLLOUT | POLLWRNORM | POLLWRBAND))
8843412Snewton	    pfd[idx].revents |= (POLLOUT | POLLWRNORM | POLLWRBAND);
8943412Snewton     }
90107849Salfred     if ((cerr = copyout(pfd, uap->fds, siz)) != 0) {
9143412Snewton       error = cerr;
9243412Snewton       goto done;   /* yeah, I know it's the next line, but this way I won't
9343412Snewton		       forget to update it if I add more code */
9443412Snewton     }
9543412Snewtondone:
9643412Snewton     free(pfd, M_TEMP);
9743412Snewton     return error;
9843412Snewton}
9943412Snewton
10043412Snewton#if defined(READ_TEST)
10143412Snewtonint
10283366Sjuliansvr4_sys_read(td, uap)
10383366Sjulian     struct thread *td;
10443412Snewton     struct svr4_sys_read_args *uap;
10543412Snewton{
10643412Snewton     struct read_args ra;
107255219Spjd     cap_rights_t rights;
10843412Snewton     struct file *fp;
10943412Snewton     struct socket *so = NULL;
11043412Snewton     int so_state;
11143412Snewton     sigset_t sigmask;
11243412Snewton     int rv;
11343412Snewton
114107849Salfred     ra.fd = uap->fd;
115107849Salfred     ra.buf = uap->buf;
116107849Salfred     ra.nbyte = uap->nbyte;
11743412Snewton
118255219Spjd     if (fget(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp) != 0) {
11943412Snewton       DPRINTF(("Something fishy with the user-supplied file descriptor...\n"));
12043412Snewton       return EBADF;
12143412Snewton     }
12243412Snewton
12343412Snewton     if (fp->f_type == DTYPE_SOCKET) {
124109153Sdillon       so = fp->f_data;
125107849Salfred       DPRINTF(("fd %d is a socket\n", uap->fd));
12697658Stanimura       if (so->so_state & SS_ASYNC) {
127107849Salfred	 DPRINTF(("fd %d is an ASYNC socket!\n", uap->fd));
12897658Stanimura       }
12997658Stanimura       DPRINTF(("Here are its flags: 0x%x\n", so->so_state));
13097658Stanimura#if defined(GROTTY_READ_HACK)
13196972Stanimura       so_state = so->so_state;
13296972Stanimura       so->so_state &= ~SS_NBIO;
13396972Stanimura#endif
13443412Snewton     }
13543412Snewton
13683366Sjulian     rv = read(td, &ra);
13743412Snewton
13843412Snewton     DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n",
139107849Salfred	     uap->fd, uap->buf, uap->nbyte, rv));
14043412Snewton     if (rv == EAGAIN) {
141114983Sjhb#ifdef DEBUG_SVR4
142114983Sjhb       struct sigacts *ps;
143114983Sjhb
144114983Sjhb       PROC_LOCK(td->td_proc);
145114983Sjhb       ps = td->td_proc->p_sigacts;
146114983Sjhb       mtx_lock(&ps->ps_mtx);
147114983Sjhb#endif
148112888Sjeff       DPRINTF(("sigmask = 0x%x\n", td->td_sigmask));
149114983Sjhb       DPRINTF(("sigignore = 0x%x\n", ps->ps_sigignore));
150114983Sjhb       DPRINTF(("sigcaught = 0x%x\n", ps->ps_sigcatch));
151112888Sjeff       DPRINTF(("siglist = 0x%x\n", td->td_siglist));
152114983Sjhb#ifdef DEBUG_SVR4
153114983Sjhb       mtx_unlock(&ps->ps_mtx);
154114983Sjhb       PROC_UNLOCK(td->td_proc);
155114983Sjhb#endif
15643412Snewton     }
15743412Snewton
15843412Snewton#if defined(GROTTY_READ_HACK)
15943412Snewton     if (so) {  /* We've already checked to see if this is a socket */
16043412Snewton       so->so_state = so_state;
16143412Snewton     }
16243412Snewton#endif
16389306Salfred     fdrop(fp, td);
16443412Snewton
16543412Snewton     return(rv);
16643412Snewton}
16743412Snewton#endif /* READ_TEST */
16843412Snewton
16943412Snewton#if defined(BOGUS)
17043412Snewtonint
17183366Sjuliansvr4_sys_write(td, uap)
17283366Sjulian     struct thread *td;
17343412Snewton     struct svr4_sys_write_args *uap;
17443412Snewton{
17543412Snewton     struct write_args wa;
17643412Snewton     struct file *fp;
17743412Snewton     int rv;
17843412Snewton
179107849Salfred     wa.fd = uap->fd;
180107849Salfred     wa.buf = uap->buf;
181107849Salfred     wa.nbyte = uap->nbyte;
18243412Snewton
18383366Sjulian     rv = write(td, &wa);
18443412Snewton
18543412Snewton     DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n",
186107849Salfred	     uap->fd, uap->buf, uap->nbyte, rv));
18743412Snewton
18843412Snewton     return(rv);
18943412Snewton}
19043412Snewton#endif /* BOGUS */
19143412Snewton
19243412Snewtonint
19383366Sjuliansvr4_fil_ioctl(fp, td, retval, fd, cmd, data)
19443412Snewton	struct file *fp;
19583366Sjulian	struct thread *td;
19643412Snewton	register_t *retval;
19743412Snewton	int fd;
19843412Snewton	u_long cmd;
19943412Snewton	caddr_t data;
20043412Snewton{
20183366Sjulian	struct filedesc *fdp = td->td_proc->p_fd;
202247602Spjd	struct filedescent *fde;
203247602Spjd	int error, num;
20443412Snewton
20543412Snewton	*retval = 0;
20643412Snewton
20743412Snewton	switch (cmd) {
20843412Snewton	case SVR4_FIOCLEX:
209168355Srwatson		FILEDESC_XLOCK(fdp);
210247602Spjd		fde = &fdp->fd_ofiles[fd];
211247602Spjd		fde->fde_flags |= UF_EXCLOSE;
212168355Srwatson		FILEDESC_XUNLOCK(fdp);
21343412Snewton		return 0;
21443412Snewton
21543412Snewton	case SVR4_FIONCLEX:
216168355Srwatson		FILEDESC_XLOCK(fdp);
217247602Spjd		fde = &fdp->fd_ofiles[fd];
218247602Spjd		fde->fde_flags &= ~UF_EXCLOSE;
219168355Srwatson		FILEDESC_XUNLOCK(fdp);
22043412Snewton		return 0;
22143412Snewton
22243412Snewton	case SVR4_FIOGETOWN:
22343412Snewton	case SVR4_FIOSETOWN:
22443412Snewton	case SVR4_FIOASYNC:
22543412Snewton	case SVR4_FIONBIO:
22643412Snewton	case SVR4_FIONREAD:
22743412Snewton		if ((error = copyin(data, &num, sizeof(num))) != 0)
22843412Snewton			return error;
22943412Snewton
23043412Snewton		switch (cmd) {
23143412Snewton		case SVR4_FIOGETOWN:	cmd = FIOGETOWN; break;
23243412Snewton		case SVR4_FIOSETOWN:	cmd = FIOSETOWN; break;
23343412Snewton		case SVR4_FIOASYNC:	cmd = FIOASYNC;  break;
23443412Snewton		case SVR4_FIONBIO:	cmd = FIONBIO;   break;
23543412Snewton		case SVR4_FIONREAD:	cmd = FIONREAD;  break;
23643412Snewton		}
23743412Snewton
23843412Snewton#ifdef SVR4_DEBUG
23943412Snewton		if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n"));
24043412Snewton#endif
241102003Srwatson		error = fo_ioctl(fp, cmd, (caddr_t) &num, td->td_ucred, td);
24243412Snewton
24343412Snewton		if (error)
24443412Snewton			return error;
24543412Snewton
24643412Snewton		return copyout(&num, data, sizeof(num));
24743412Snewton
24843412Snewton	default:
24943412Snewton		DPRINTF(("Unknown svr4 filio %lx\n", cmd));
25043412Snewton		return 0;	/* ENOSYS really */
25143412Snewton	}
25243412Snewton}
253