svr4_filio.c revision 139743
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 139743 2005-01-05 22:34:37Z imp $");
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>
43121275Stjr#include <sys/resource.h>
44121275Stjr#include <sys/resourcevar.h>
4543412Snewton
4643412Snewton#include <sys/sysproto.h>
4743412Snewton
4865302Sobrien#include <compat/svr4/svr4.h>
4965302Sobrien#include <compat/svr4/svr4_types.h>
5065302Sobrien#include <compat/svr4/svr4_util.h>
5165302Sobrien#include <compat/svr4/svr4_signal.h>
5265302Sobrien#include <compat/svr4/svr4_proto.h>
5365302Sobrien#include <compat/svr4/svr4_ioctl.h>
5465302Sobrien#include <compat/svr4/svr4_filio.h>
5543412Snewton
5643412Snewton/*#define GROTTY_READ_HACK*/
5743412Snewton
5843412Snewtonint
5983366Sjuliansvr4_sys_poll(td, uap)
6083366Sjulian     struct thread *td;
6143412Snewton     struct svr4_sys_poll_args *uap;
6243412Snewton{
6343412Snewton     int error;
6443412Snewton     struct poll_args pa;
6543412Snewton     struct pollfd *pfd;
6643412Snewton     int idx = 0, cerr;
6743412Snewton     u_long siz;
6843412Snewton
69125454Sjhb     PROC_LOCK(td->td_proc);
70125454Sjhb     if (uap->nfds > lim_cur(td->td_proc, RLIMIT_NOFILE) &&
71125454Sjhb       uap->nfds > FD_SETSIZE) {
72125454Sjhb       PROC_UNLOCK(td->td_proc);
73125454Sjhb       return (EINVAL);
74125454Sjhb     }
75125454Sjhb     PROC_UNLOCK(td->td_proc);
76121275Stjr
77107849Salfred     pa.fds = uap->fds;
78107849Salfred     pa.nfds = uap->nfds;
79107849Salfred     pa.timeout = uap->timeout;
8043412Snewton
81107849Salfred     siz = uap->nfds * sizeof(struct pollfd);
82111119Simp     pfd = (struct pollfd *)malloc(siz, M_TEMP, M_WAITOK);
8343412Snewton
8483366Sjulian     error = poll(td, (struct poll_args *)uap);
8543412Snewton
86107849Salfred     if ((cerr = copyin(uap->fds, pfd, siz)) != 0) {
8743412Snewton       error = cerr;
8843412Snewton       goto done;
8943412Snewton     }
9043412Snewton
91107849Salfred     for (idx = 0; idx < uap->nfds; idx++) {
9243412Snewton       /* POLLWRNORM already equals POLLOUT, so we don't worry about that */
9343412Snewton       if (pfd[idx].revents & (POLLOUT | POLLWRNORM | POLLWRBAND))
9443412Snewton	    pfd[idx].revents |= (POLLOUT | POLLWRNORM | POLLWRBAND);
9543412Snewton     }
96107849Salfred     if ((cerr = copyout(pfd, uap->fds, siz)) != 0) {
9743412Snewton       error = cerr;
9843412Snewton       goto done;   /* yeah, I know it's the next line, but this way I won't
9943412Snewton		       forget to update it if I add more code */
10043412Snewton     }
10143412Snewtondone:
10243412Snewton     free(pfd, M_TEMP);
10343412Snewton     return error;
10443412Snewton}
10543412Snewton
10643412Snewton#if defined(READ_TEST)
10743412Snewtonint
10883366Sjuliansvr4_sys_read(td, uap)
10983366Sjulian     struct thread *td;
11043412Snewton     struct svr4_sys_read_args *uap;
11143412Snewton{
11243412Snewton     struct read_args ra;
11343412Snewton     struct file *fp;
11443412Snewton     struct socket *so = NULL;
11543412Snewton     int so_state;
11643412Snewton     sigset_t sigmask;
11743412Snewton     int rv;
11843412Snewton
119107849Salfred     ra.fd = uap->fd;
120107849Salfred     ra.buf = uap->buf;
121107849Salfred     ra.nbyte = uap->nbyte;
12243412Snewton
12389319Salfred     if (fget(td, uap->fd, &fp) != 0) {
12443412Snewton       DPRINTF(("Something fishy with the user-supplied file descriptor...\n"));
12543412Snewton       return EBADF;
12643412Snewton     }
12743412Snewton
12843412Snewton     if (fp->f_type == DTYPE_SOCKET) {
129109153Sdillon       so = fp->f_data;
130107849Salfred       DPRINTF(("fd %d is a socket\n", uap->fd));
13197658Stanimura       if (so->so_state & SS_ASYNC) {
132107849Salfred	 DPRINTF(("fd %d is an ASYNC socket!\n", uap->fd));
13397658Stanimura       }
13497658Stanimura       DPRINTF(("Here are its flags: 0x%x\n", so->so_state));
13597658Stanimura#if defined(GROTTY_READ_HACK)
13696972Stanimura       so_state = so->so_state;
13796972Stanimura       so->so_state &= ~SS_NBIO;
13896972Stanimura#endif
13943412Snewton     }
14043412Snewton
14183366Sjulian     rv = read(td, &ra);
14243412Snewton
14343412Snewton     DPRINTF(("svr4_read(%d, 0x%0x, %d) = %d\n",
144107849Salfred	     uap->fd, uap->buf, uap->nbyte, rv));
14543412Snewton     if (rv == EAGAIN) {
146114983Sjhb#ifdef DEBUG_SVR4
147114983Sjhb       struct sigacts *ps;
148114983Sjhb
149114983Sjhb       PROC_LOCK(td->td_proc);
150114983Sjhb       ps = td->td_proc->p_sigacts;
151114983Sjhb       mtx_lock(&ps->ps_mtx);
152114983Sjhb#endif
153112888Sjeff       DPRINTF(("sigmask = 0x%x\n", td->td_sigmask));
154114983Sjhb       DPRINTF(("sigignore = 0x%x\n", ps->ps_sigignore));
155114983Sjhb       DPRINTF(("sigcaught = 0x%x\n", ps->ps_sigcatch));
156112888Sjeff       DPRINTF(("siglist = 0x%x\n", td->td_siglist));
157114983Sjhb#ifdef DEBUG_SVR4
158114983Sjhb       mtx_unlock(&ps->ps_mtx);
159114983Sjhb       PROC_UNLOCK(td->td_proc);
160114983Sjhb#endif
16143412Snewton     }
16243412Snewton
16343412Snewton#if defined(GROTTY_READ_HACK)
16443412Snewton     if (so) {  /* We've already checked to see if this is a socket */
16543412Snewton       so->so_state = so_state;
16643412Snewton     }
16743412Snewton#endif
16889306Salfred     fdrop(fp, td);
16943412Snewton
17043412Snewton     return(rv);
17143412Snewton}
17243412Snewton#endif /* READ_TEST */
17343412Snewton
17443412Snewton#if defined(BOGUS)
17543412Snewtonint
17683366Sjuliansvr4_sys_write(td, uap)
17783366Sjulian     struct thread *td;
17843412Snewton     struct svr4_sys_write_args *uap;
17943412Snewton{
18043412Snewton     struct write_args wa;
18143412Snewton     struct file *fp;
18243412Snewton     int rv;
18343412Snewton
184107849Salfred     wa.fd = uap->fd;
185107849Salfred     wa.buf = uap->buf;
186107849Salfred     wa.nbyte = uap->nbyte;
18743412Snewton
18883366Sjulian     rv = write(td, &wa);
18943412Snewton
19043412Snewton     DPRINTF(("svr4_write(%d, 0x%0x, %d) = %d\n",
191107849Salfred	     uap->fd, uap->buf, uap->nbyte, rv));
19243412Snewton
19343412Snewton     return(rv);
19443412Snewton}
19543412Snewton#endif /* BOGUS */
19643412Snewton
19743412Snewtonint
19883366Sjuliansvr4_fil_ioctl(fp, td, retval, fd, cmd, data)
19943412Snewton	struct file *fp;
20083366Sjulian	struct thread *td;
20143412Snewton	register_t *retval;
20243412Snewton	int fd;
20343412Snewton	u_long cmd;
20443412Snewton	caddr_t data;
20543412Snewton{
20643412Snewton	int error;
20743412Snewton	int num;
20883366Sjulian	struct filedesc *fdp = td->td_proc->p_fd;
20943412Snewton
21043412Snewton	*retval = 0;
21143412Snewton
21243412Snewton	switch (cmd) {
21343412Snewton	case SVR4_FIOCLEX:
214137647Sphk		FILEDESC_LOCK_FAST(fdp);
21543412Snewton		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
216137647Sphk		FILEDESC_UNLOCK_FAST(fdp);
21743412Snewton		return 0;
21843412Snewton
21943412Snewton	case SVR4_FIONCLEX:
220137647Sphk		FILEDESC_LOCK_FAST(fdp);
22143412Snewton		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
222137647Sphk		FILEDESC_UNLOCK_FAST(fdp);
22343412Snewton		return 0;
22443412Snewton
22543412Snewton	case SVR4_FIOGETOWN:
22643412Snewton	case SVR4_FIOSETOWN:
22743412Snewton	case SVR4_FIOASYNC:
22843412Snewton	case SVR4_FIONBIO:
22943412Snewton	case SVR4_FIONREAD:
23043412Snewton		if ((error = copyin(data, &num, sizeof(num))) != 0)
23143412Snewton			return error;
23243412Snewton
23343412Snewton		switch (cmd) {
23443412Snewton		case SVR4_FIOGETOWN:	cmd = FIOGETOWN; break;
23543412Snewton		case SVR4_FIOSETOWN:	cmd = FIOSETOWN; break;
23643412Snewton		case SVR4_FIOASYNC:	cmd = FIOASYNC;  break;
23743412Snewton		case SVR4_FIONBIO:	cmd = FIONBIO;   break;
23843412Snewton		case SVR4_FIONREAD:	cmd = FIONREAD;  break;
23943412Snewton		}
24043412Snewton
24143412Snewton#ifdef SVR4_DEBUG
24243412Snewton		if (cmd == FIOASYNC) DPRINTF(("FIOASYNC\n"));
24343412Snewton#endif
244102003Srwatson		error = fo_ioctl(fp, cmd, (caddr_t) &num, td->td_ucred, td);
24543412Snewton
24643412Snewton		if (error)
24743412Snewton			return error;
24843412Snewton
24943412Snewton		return copyout(&num, data, sizeof(num));
25043412Snewton
25143412Snewton	default:
25243412Snewton		DPRINTF(("Unknown svr4 filio %lx\n", cmd));
25343412Snewton		return 0;	/* ENOSYS really */
25443412Snewton	}
25543412Snewton}
256