#include <sys/param.h>
#include <sys/systm.h>
#include <sys/filedesc.h>
#include <sys/ioctl.h>
#include <sys/file_internal.h>
#include <sys/proc_internal.h>
#include <sys/socketvar.h>
#include <sys/uio_internal.h>
#include <sys/kernel.h>
#include <sys/stat.h>
#include <sys/malloc.h>
#include <sys/sysproto.h>
#include <sys/mount_internal.h>
#include <sys/protosw.h>
#include <sys/ev.h>
#include <sys/user.h>
#include <sys/kdebug.h>
#include <sys/poll.h>
#include <sys/event.h>
#include <sys/eventvar.h>
#include <mach/mach_types.h>
#include <kern/kern_types.h>
#include <kern/assert.h>
#include <kern/kalloc.h>
#include <kern/thread.h>
#include <kern/clock.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/errno.h>
#include <sys/syscall.h>
#include <sys/pipe.h>
#include <security/audit/audit.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcpip.h>
#include <netinet/tcp_debug.h>
#include <kern/wait_queue.h>
#include <kern/kalloc.h>
#include <sys/vnode_internal.h>
void evsofree(struct socket *);
void evpipefree(struct pipe *);
void postpipeevent(struct pipe *, int);
void postevent(struct socket *, struct sockbuf *, int);
extern kern_return_t IOBSDGetPlatformUUID(__darwin_uuid_t uuid, mach_timespec_t timeoutp);
int rd_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
int wr_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
extern void *get_bsduthreadarg(thread_t);
extern int *get_bsduthreadrval(thread_t);
__private_extern__ int dofileread(vfs_context_t ctx, struct fileproc *fp,
user_addr_t bufp, user_size_t nbyte,
off_t offset, int flags, user_ssize_t *retval);
__private_extern__ int dofilewrite(vfs_context_t ctx, struct fileproc *fp,
user_addr_t bufp, user_size_t nbyte,
off_t offset, int flags, user_ssize_t *retval);
__private_extern__ int preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_vnode);
__private_extern__ void donefileread(struct proc *p, struct fileproc *fp_ret, int fd);
#if NETAT
extern int appletalk_inited;
#endif
#define f_flag f_fglob->fg_flag
#define f_type f_fglob->fg_type
#define f_msgcount f_fglob->fg_msgcount
#define f_cred f_fglob->fg_cred
#define f_ops f_fglob->fg_ops
#define f_offset f_fglob->fg_offset
#define f_data f_fglob->fg_data
int
read(struct proc *p, struct read_args *uap, user_ssize_t *retval)
{
__pthread_testcancel(1);
return(read_nocancel(p, (struct read_nocancel_args *)uap, retval));
}
int
read_nocancel(struct proc *p, struct read_nocancel_args *uap, user_ssize_t *retval)
{
struct fileproc *fp;
int error;
int fd = uap->fd;
struct vfs_context context;
if ( (error = preparefileread(p, &fp, fd, 0)) )
return (error);
context = *(vfs_context_current());
context.vc_ucred = fp->f_fglob->fg_cred;
error = dofileread(&context, fp, uap->cbuf, uap->nbyte,
(off_t)-1, 0, retval);
donefileread(p, fp, fd);
return (error);
}
int
pread(struct proc *p, struct pread_args *uap, user_ssize_t *retval)
{
__pthread_testcancel(1);
return(pread_nocancel(p, (struct pread_nocancel_args *)uap, retval));
}
int
pread_nocancel(struct proc *p, struct pread_nocancel_args *uap, user_ssize_t *retval)
{
struct fileproc *fp = NULL;
int fd = uap->fd;
int error;
struct vfs_context context;
if ( (error = preparefileread(p, &fp, fd, 1)) )
goto out;
context = *(vfs_context_current());
context.vc_ucred = fp->f_fglob->fg_cred;
error = dofileread(&context, fp, uap->buf, uap->nbyte,
uap->offset, FOF_OFFSET, retval);
donefileread(p, fp, fd);
KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pread) | DBG_FUNC_NONE),
uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
out:
return (error);
}
void
donefileread(struct proc *p, struct fileproc *fp, int fd)
{
proc_fdlock_spin(p);
fp->f_flags &= ~FP_INCHRREAD;
fp_drop(p, fd, fp, 1);
proc_fdunlock(p);
}
int
preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_pread)
{
vnode_t vp;
int error;
struct fileproc *fp;
AUDIT_ARG(fd, fd);
proc_fdlock_spin(p);
error = fp_lookup(p, fd, &fp, 1);
if (error) {
proc_fdunlock(p);
return (error);
}
if ((fp->f_flag & FREAD) == 0) {
error = EBADF;
goto out;
}
if (check_for_pread && (fp->f_type != DTYPE_VNODE)) {
error = ESPIPE;
goto out;
}
if (fp->f_type == DTYPE_VNODE) {
vp = (struct vnode *)fp->f_fglob->fg_data;
if (check_for_pread && (vnode_isfifo(vp))) {
error = ESPIPE;
goto out;
}
if (check_for_pread && (vp->v_flag & VISTTY)) {
error = ENXIO;
goto out;
}
if (vp->v_type == VCHR)
fp->f_flags |= FP_INCHRREAD;
}
*fp_ret = fp;
proc_fdunlock(p);
return (0);
out:
fp_drop(p, fd, fp, 1);
proc_fdunlock(p);
return (error);
}
__private_extern__ int
dofileread(vfs_context_t ctx, struct fileproc *fp,
user_addr_t bufp, user_size_t nbyte, off_t offset, int flags,
user_ssize_t *retval)
{
uio_t auio;
user_ssize_t bytecnt;
long error = 0;
char uio_buf[ UIO_SIZEOF(1) ];
if (nbyte > INT_MAX)
return (EINVAL);
if (IS_64BIT_PROCESS(vfs_context_proc(ctx))) {
auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_READ,
&uio_buf[0], sizeof(uio_buf));
} else {
auio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_READ,
&uio_buf[0], sizeof(uio_buf));
}
uio_addiov(auio, bufp, nbyte);
bytecnt = nbyte;
if ((error = fo_read(fp, auio, flags, ctx))) {
if (uio_resid(auio) != bytecnt && (error == ERESTART ||
error == EINTR || error == EWOULDBLOCK))
error = 0;
}
bytecnt -= uio_resid(auio);
*retval = bytecnt;
return (error);
}
int
readv(struct proc *p, struct readv_args *uap, user_ssize_t *retval)
{
__pthread_testcancel(1);
return(readv_nocancel(p, (struct readv_nocancel_args *)uap, retval));
}
int
readv_nocancel(struct proc *p, struct readv_nocancel_args *uap, user_ssize_t *retval)
{
uio_t auio = NULL;
int error;
struct user_iovec *iovp;
if (uap->iovcnt <= 0 || uap->iovcnt > UIO_MAXIOV)
return (EINVAL);
auio = uio_create(uap->iovcnt, 0,
(IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
UIO_READ);
iovp = uio_iovsaddr(auio);
if (iovp == NULL) {
error = ENOMEM;
goto ExitThisRoutine;
}
error = copyin_user_iovec_array(uap->iovp,
IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32,
uap->iovcnt, iovp);
if (error) {
goto ExitThisRoutine;
}
uio_calculateresid(auio);
error = rd_uio(p, uap->fd, auio, retval);
ExitThisRoutine:
if (auio != NULL) {
uio_free(auio);
}
return (error);
}
int
write(struct proc *p, struct write_args *uap, user_ssize_t *retval)
{
__pthread_testcancel(1);
return(write_nocancel(p, (struct write_nocancel_args *)uap, retval));
}
int
write_nocancel(struct proc *p, struct write_nocancel_args *uap, user_ssize_t *retval)
{
struct fileproc *fp;
int error;
int fd = uap->fd;
AUDIT_ARG(fd, fd);
error = fp_lookup(p,fd,&fp,0);
if (error)
return(error);
if ((fp->f_flag & FWRITE) == 0) {
error = EBADF;
} else {
struct vfs_context context = *(vfs_context_current());
context.vc_ucred = fp->f_fglob->fg_cred;
error = dofilewrite(&context, fp, uap->cbuf, uap->nbyte,
(off_t)-1, 0, retval);
}
if (error == 0)
fp_drop_written(p, fd, fp);
else
fp_drop(p, fd, fp, 0);
return(error);
}
int
pwrite(struct proc *p, struct pwrite_args *uap, user_ssize_t *retval)
{
__pthread_testcancel(1);
return(pwrite_nocancel(p, (struct pwrite_nocancel_args *)uap, retval));
}
int
pwrite_nocancel(struct proc *p, struct pwrite_nocancel_args *uap, user_ssize_t *retval)
{
struct fileproc *fp;
int error;
int fd = uap->fd;
vnode_t vp = (vnode_t)0;
AUDIT_ARG(fd, fd);
error = fp_lookup(p,fd,&fp,0);
if (error)
return(error);
if ((fp->f_flag & FWRITE) == 0) {
error = EBADF;
} else {
struct vfs_context context = *vfs_context_current();
context.vc_ucred = fp->f_fglob->fg_cred;
if (fp->f_type != DTYPE_VNODE) {
error = ESPIPE;
goto errout;
}
vp = (vnode_t)fp->f_fglob->fg_data;
if (vnode_isfifo(vp)) {
error = ESPIPE;
goto errout;
}
if ((vp->v_flag & VISTTY)) {
error = ENXIO;
goto errout;
}
if (uap->offset == (off_t)-1) {
error = EINVAL;
goto errout;
}
error = dofilewrite(&context, fp, uap->buf, uap->nbyte,
uap->offset, FOF_OFFSET, retval);
}
errout:
if (error == 0)
fp_drop_written(p, fd, fp);
else
fp_drop(p, fd, fp, 0);
KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pwrite) | DBG_FUNC_NONE),
uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
return(error);
}
__private_extern__ int
dofilewrite(vfs_context_t ctx, struct fileproc *fp,
user_addr_t bufp, user_size_t nbyte, off_t offset, int flags,
user_ssize_t *retval)
{
uio_t auio;
long error = 0;
user_ssize_t bytecnt;
char uio_buf[ UIO_SIZEOF(1) ];
if (nbyte > INT_MAX)
return (EINVAL);
if (IS_64BIT_PROCESS(vfs_context_proc(ctx))) {
auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_WRITE,
&uio_buf[0], sizeof(uio_buf));
} else {
auio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_WRITE,
&uio_buf[0], sizeof(uio_buf));
}
uio_addiov(auio, bufp, nbyte);
bytecnt = nbyte;
if ((error = fo_write(fp, auio, flags, ctx))) {
if (uio_resid(auio) != bytecnt && (error == ERESTART ||
error == EINTR || error == EWOULDBLOCK))
error = 0;
if (error == EPIPE && fp->f_type != DTYPE_SOCKET) {
psignal(vfs_context_proc(ctx), SIGPIPE);
}
}
bytecnt -= uio_resid(auio);
*retval = bytecnt;
return (error);
}
int
writev(struct proc *p, struct writev_args *uap, user_ssize_t *retval)
{
__pthread_testcancel(1);
return(writev_nocancel(p, (struct writev_nocancel_args *)uap, retval));
}
int
writev_nocancel(struct proc *p, struct writev_nocancel_args *uap, user_ssize_t *retval)
{
uio_t auio = NULL;
int error;
struct user_iovec *iovp;
AUDIT_ARG(fd, uap->fd);
if (uap->iovcnt <= 0 || uap->iovcnt > UIO_MAXIOV)
return (EINVAL);
auio = uio_create(uap->iovcnt, 0,
(IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
UIO_WRITE);
iovp = uio_iovsaddr(auio);
if (iovp == NULL) {
error = ENOMEM;
goto ExitThisRoutine;
}
error = copyin_user_iovec_array(uap->iovp,
IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32,
uap->iovcnt, iovp);
if (error) {
goto ExitThisRoutine;
}
uio_calculateresid(auio);
error = wr_uio(p, uap->fd, auio, retval);
ExitThisRoutine:
if (auio != NULL) {
uio_free(auio);
}
return (error);
}
int
wr_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval)
{
struct fileproc *fp;
int error;
user_ssize_t count;
struct vfs_context context = *vfs_context_current();
error = fp_lookup(p,fdes,&fp,0);
if (error)
return(error);
if ((fp->f_flag & FWRITE) == 0) {
error = EBADF;
goto out;
}
count = uio_resid(uio);
context.vc_ucred = fp->f_cred;
error = fo_write(fp, uio, 0, &context);
if (error) {
if (uio_resid(uio) != count && (error == ERESTART ||
error == EINTR || error == EWOULDBLOCK))
error = 0;
if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
psignal(p, SIGPIPE);
}
*retval = count - uio_resid(uio);
out:
if ( (error == 0) )
fp_drop_written(p, fdes, fp);
else
fp_drop(p, fdes, fp, 0);
return(error);
}
int
rd_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval)
{
struct fileproc *fp;
int error;
user_ssize_t count;
struct vfs_context context = *vfs_context_current();
if ( (error = preparefileread(p, &fp, fdes, 0)) )
return (error);
count = uio_resid(uio);
context.vc_ucred = fp->f_cred;
error = fo_read(fp, uio, 0, &context);
if (error) {
if (uio_resid(uio) != count && (error == ERESTART ||
error == EINTR || error == EWOULDBLOCK))
error = 0;
}
*retval = count - uio_resid(uio);
donefileread(p, fp, fdes);
return (error);
}
int
ioctl(struct proc *p, struct ioctl_args *uap, __unused int32_t *retval)
{
struct fileproc *fp;
u_long com;
int error = 0;
u_int size;
caddr_t datap, memp;
boolean_t is64bit;
int tmp;
#define STK_PARAMS 128
char stkbuf[STK_PARAMS];
int fd = uap->fd;
struct vfs_context context = *vfs_context_current();
AUDIT_ARG(fd, uap->fd);
AUDIT_ARG(addr, uap->data);
is64bit = proc_is64bit(p);
#if CONFIG_AUDIT
if (is64bit)
AUDIT_ARG(value64, uap->com);
else
AUDIT_ARG(cmd, CAST_DOWN_EXPLICIT(int, uap->com));
#endif
proc_fdlock(p);
error = fp_lookup(p,fd,&fp,1);
if (error) {
proc_fdunlock(p);
return(error);
}
AUDIT_ARG(file, p, fp);
if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
error = EBADF;
goto out;
}
context.vc_ucred = fp->f_fglob->fg_cred;
#if CONFIG_MACF
error = mac_file_check_ioctl(context.vc_ucred, fp->f_fglob, uap->com);
if (error)
goto out;
#endif
#if NETAT
{
if (appletalk_inited && ((uap->com & 0x0000FFFF) == 0xff99)) {
u_long fixed_command;
#ifdef APPLETALK_DEBUG
kprintf("ioctl: special AppleTalk \n");
#endif
datap = &stkbuf[0];
*(user_addr_t *)datap = uap->data;
fixed_command = _IOW(0, 0xff99, uap->data);
error = fo_ioctl(fp, fixed_command, datap, &context);
goto out;
}
}
#endif
switch (com = uap->com) {
case FIONCLEX:
*fdflags(p, uap->fd) &= ~UF_EXCLOSE;
error =0;
goto out;
case FIOCLEX:
*fdflags(p, uap->fd) |= UF_EXCLOSE;
error =0;
goto out;
}
size = IOCPARM_LEN(com);
if (size > IOCPARM_MAX) {
error = ENOTTY;
goto out;
}
memp = NULL;
if (size > sizeof (stkbuf)) {
proc_fdunlock(p);
if ((memp = (caddr_t)kalloc(size)) == 0) {
proc_fdlock(p);
error = ENOMEM;
goto out;
}
proc_fdlock(p);
datap = memp;
} else
datap = &stkbuf[0];
if (com&IOC_IN) {
if (size) {
proc_fdunlock(p);
error = copyin(uap->data, datap, size);
if (error) {
if (memp)
kfree(memp, size);
proc_fdlock(p);
goto out;
}
proc_fdlock(p);
} else {
if (is64bit) {
*(user_addr_t *)datap = uap->data;
}
else {
*(uint32_t *)datap = (uint32_t)uap->data;
}
}
} else if ((com&IOC_OUT) && size)
bzero(datap, size);
else if (com&IOC_VOID) {
if (is64bit) {
*(user_addr_t *)datap = uap->data;
}
else {
*(uint32_t *)datap = (uint32_t)uap->data;
}
}
switch (com) {
case FIONBIO:
if ( (tmp = *(int *)datap) )
fp->f_flag |= FNONBLOCK;
else
fp->f_flag &= ~FNONBLOCK;
error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, &context);
break;
case FIOASYNC:
if ( (tmp = *(int *)datap) )
fp->f_flag |= FASYNC;
else
fp->f_flag &= ~FASYNC;
error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, &context);
break;
case FIOSETOWN:
tmp = *(int *)datap;
if (fp->f_type == DTYPE_SOCKET) {
((struct socket *)fp->f_data)->so_pgid = tmp;
error = 0;
break;
}
if (fp->f_type == DTYPE_PIPE) {
error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, &context);
break;
}
if (tmp <= 0) {
tmp = -tmp;
} else {
struct proc *p1 = proc_find(tmp);
if (p1 == 0) {
error = ESRCH;
break;
}
tmp = p1->p_pgrpid;
proc_rele(p1);
}
error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, &context);
break;
case FIOGETOWN:
if (fp->f_type == DTYPE_SOCKET) {
error = 0;
*(int *)datap = ((struct socket *)fp->f_data)->so_pgid;
break;
}
error = fo_ioctl(fp, TIOCGPGRP, datap, &context);
*(int *)datap = -*(int *)datap;
break;
default:
error = fo_ioctl(fp, com, datap, &context);
if (error == 0 && (com&IOC_OUT) && size)
error = copyout(datap, uap->data, (u_int)size);
break;
}
proc_fdunlock(p);
if (memp)
kfree(memp, size);
proc_fdlock(p);
out:
fp_drop(p, fd, fp, 1);
proc_fdunlock(p);
return(error);
}
int selwait, nselcoll;
#define SEL_FIRSTPASS 1
#define SEL_SECONDPASS 2
extern int selcontinue(int error);
extern int selprocess(int error, int sel_pass);
static int selscan(struct proc *p, struct _select * sel,
int nfd, int32_t *retval, int sel_pass, wait_queue_sub_t wqsub);
static int selcount(struct proc *p, u_int32_t *ibits, u_int32_t *obits,
int nfd, int * count, int *kfcount);
static int seldrop(struct proc *p, u_int32_t *ibits, int nfd);
int
select(struct proc *p, struct select_args *uap, int32_t *retval)
{
__pthread_testcancel(1);
return(select_nocancel(p, (struct select_nocancel_args *)uap, retval));
}
int
select_nocancel(struct proc *p, struct select_nocancel_args *uap, int32_t *retval)
{
int error = 0;
u_int ni, nw, size;
thread_t th_act;
struct uthread *uth;
struct _select *sel;
int needzerofill = 1;
int count = 0;
int kfcount = 0;
th_act = current_thread();
uth = get_bsdthread_info(th_act);
sel = &uth->uu_select;
retval = (int *)get_bsduthreadrval(th_act);
*retval = 0;
if (uap->nd < 0) {
return (EINVAL);
}
if (p->p_fd == NULL) {
return (EBADF);
}
if (uap->nd > p->p_fd->fd_nfiles)
uap->nd = p->p_fd->fd_nfiles;
nw = howmany(uap->nd, NFDBITS);
ni = nw * sizeof(fd_mask);
if (sel->nbytes < (3 * ni)) {
int nbytes = 3 * ni;
if (sel->ibits != NULL)
FREE(sel->ibits, M_TEMP);
if (sel->obits != NULL) {
FREE(sel->obits, M_TEMP);
sel->obits = NULL;
}
MALLOC(sel->ibits, u_int32_t *, nbytes, M_TEMP, M_WAITOK | M_ZERO);
if (sel->ibits == NULL)
return (EAGAIN);
MALLOC(sel->obits, u_int32_t *, nbytes, M_TEMP, M_WAITOK | M_ZERO);
if (sel->obits == NULL) {
FREE(sel->ibits, M_TEMP);
sel->ibits = NULL;
return (EAGAIN);
}
sel->nbytes = nbytes;
needzerofill = 0;
}
if (needzerofill) {
bzero((caddr_t)sel->ibits, sel->nbytes);
bzero((caddr_t)sel->obits, sel->nbytes);
}
#define getbits(name, x) \
do { \
if (uap->name && (error = copyin(uap->name, \
(caddr_t)&sel->ibits[(x) * nw], ni))) \
goto continuation; \
} while (0)
getbits(in, 0);
getbits(ou, 1);
getbits(ex, 2);
#undef getbits
if (uap->tv) {
struct timeval atv;
if (IS_64BIT_PROCESS(p)) {
struct user64_timeval atv64;
error = copyin(uap->tv, (caddr_t)&atv64, sizeof(atv64));
atv.tv_sec = atv64.tv_sec;
atv.tv_usec = atv64.tv_usec;
} else {
struct user32_timeval atv32;
error = copyin(uap->tv, (caddr_t)&atv32, sizeof(atv32));
atv.tv_sec = atv32.tv_sec;
atv.tv_usec = atv32.tv_usec;
}
if (error)
goto continuation;
if (itimerfix(&atv)) {
error = EINVAL;
goto continuation;
}
clock_absolutetime_interval_to_deadline(
tvtoabstime(&atv), &sel->abstime);
}
else
sel->abstime = 0;
sel->kfcount = 0;
if ( (error = selcount(p, sel->ibits, sel->obits, uap->nd, &count, &kfcount)) ) {
goto continuation;
}
sel->count = count;
sel->kfcount = kfcount;
size = SIZEOF_WAITQUEUE_SET + (count * SIZEOF_WAITQUEUE_LINK);
if (uth->uu_allocsize) {
if (uth->uu_wqset == 0)
panic("select: wql memory smashed");
if (size > uth->uu_allocsize) {
kfree(uth->uu_wqset, uth->uu_allocsize);
uth->uu_allocsize = size;
uth->uu_wqset = (wait_queue_set_t)kalloc(size);
if (uth->uu_wqset == (wait_queue_set_t)NULL)
panic("failed to allocate memory for waitqueue\n");
}
} else {
sel->count = count;
uth->uu_allocsize = size;
uth->uu_wqset = (wait_queue_set_t)kalloc(uth->uu_allocsize);
if (uth->uu_wqset == (wait_queue_set_t)NULL)
panic("failed to allocate memory for waitqueue\n");
}
bzero(uth->uu_wqset, size);
sel->wql = (char *)uth->uu_wqset + SIZEOF_WAITQUEUE_SET;
wait_queue_set_init(uth->uu_wqset, (SYNC_POLICY_FIFO | SYNC_POLICY_PREPOST));
continuation:
return selprocess(error, SEL_FIRSTPASS);
}
int
selcontinue(int error)
{
return selprocess(error, SEL_SECONDPASS);
}
int
selprocess(int error, int sel_pass)
{
int ncoll;
u_int ni, nw;
thread_t th_act;
struct uthread *uth;
struct proc *p;
struct select_args *uap;
int *retval;
struct _select *sel;
int unwind = 1;
int prepost = 0;
int somewakeup = 0;
int doretry = 0;
wait_result_t wait_result;
p = current_proc();
th_act = current_thread();
uap = (struct select_args *)get_bsduthreadarg(th_act);
retval = (int *)get_bsduthreadrval(th_act);
uth = get_bsdthread_info(th_act);
sel = &uth->uu_select;
if ((error != 0) && (sel_pass == SEL_FIRSTPASS))
unwind = 0;
if (sel->count == 0)
unwind = 0;
retry:
if (error != 0) {
goto done;
}
ncoll = nselcoll;
OSBitOrAtomic(P_SELECT, &p->p_flag);
if (sel->count) {
if (sel_pass == SEL_FIRSTPASS)
wait_queue_sub_clearrefs(uth->uu_wqset);
error = selscan(p, sel, uap->nd, retval, sel_pass, (wait_queue_sub_t)uth->uu_wqset);
if (error || *retval) {
goto done;
}
if (prepost) {
prepost = 0;
doretry = 1;
}
if (somewakeup) {
somewakeup = 0;
doretry = 1;
}
}
if (uap->tv) {
uint64_t now;
clock_get_uptime(&now);
if (now >= sel->abstime)
goto done;
}
if (doretry) {
doretry = 0;
sel_pass = SEL_FIRSTPASS;
goto retry;
}
if (uap->tv && sel->abstime == 0) {
goto done;
}
if ((sel_pass == SEL_SECONDPASS) || ((p->p_flag & P_SELECT) == 0)) {
sel_pass = SEL_FIRSTPASS;
goto retry;
}
OSBitAndAtomic(~((uint32_t)P_SELECT), &p->p_flag);
if (sel->count &&(sel_pass == SEL_SECONDPASS))
panic("selprocess: 2nd pass assertwaiting");
wait_result = wait_queue_assert_wait((wait_queue_t)uth->uu_wqset,
NULL, THREAD_ABORTSAFE, sel->abstime);
if (wait_result != THREAD_AWAKENED) {
error = tsleep1(NULL, PSOCK | PCATCH,
"select", 0, selcontinue);
} else {
prepost = 1;
error = 0;
}
sel_pass = SEL_SECONDPASS;
if (error == 0) {
if (!prepost)
somewakeup =1;
goto retry;
}
done:
if (unwind) {
wait_subqueue_unlink_all(uth->uu_wqset);
seldrop(p, sel->ibits, uap->nd);
}
OSBitAndAtomic(~((uint32_t)P_SELECT), &p->p_flag);
if (error == ERESTART)
error = EINTR;
if (error == EWOULDBLOCK)
error = 0;
nw = howmany(uap->nd, NFDBITS);
ni = nw * sizeof(fd_mask);
#define putbits(name, x) \
do { \
if (uap->name && (error2 = \
copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
error = error2; \
} while (0)
if (error == 0) {
int error2;
putbits(in, 0);
putbits(ou, 1);
putbits(ex, 2);
#undef putbits
}
return(error);
}
static int
selscan(struct proc *p, struct _select *sel, int nfd, int32_t *retval,
int sel_pass, wait_queue_sub_t wqsub)
{
struct filedesc *fdp = p->p_fd;
int msk, i, j, fd;
u_int32_t bits;
struct fileproc *fp;
int n = 0;
int nc = 0;
static int flag[3] = { FREAD, FWRITE, 0 };
u_int32_t *iptr, *optr;
u_int nw;
u_int32_t *ibits, *obits;
char * wql;
char * wql_ptr;
int count, kfcount;
vnode_t vp;
struct vfs_context context = *vfs_context_current();
if (fdp == NULL) {
*retval=0;
return(EIO);
}
ibits = sel->ibits;
obits = sel->obits;
wql = sel->wql;
nw = howmany(nfd, NFDBITS);
count = sel->count;
kfcount = sel->kfcount;
if (kfcount > count)
panic("selscan: count < kfcount");
if (kfcount != 0) {
proc_fdlock(p);
for (msk = 0; msk < 3; msk++) {
iptr = (u_int32_t *)&ibits[msk * nw];
optr = (u_int32_t *)&obits[msk * nw];
for (i = 0; i < nfd; i += NFDBITS) {
bits = iptr[i/NFDBITS];
while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
bits &= ~(1 << j);
fp = fdp->fd_ofiles[fd];
if (fp == NULL ||
(fdp->fd_ofileflags[fd] & UF_RESERVED)) {
proc_fdunlock(p);
return(EBADF);
}
if (sel_pass == SEL_SECONDPASS) {
wql_ptr = (char *)0;
fp->f_flags &= ~FP_INSELECT;
fp->f_waddr = (void *)0;
} else {
wql_ptr = (wql + nc * SIZEOF_WAITQUEUE_LINK);
fp->f_flags |= FP_INSELECT;
fp->f_waddr = (void *)wqsub;
}
context.vc_ucred = fp->f_cred;
if (fp->f_ops && (fp->f_type == DTYPE_VNODE)
&& ((vp = (struct vnode *)fp->f_data) != NULLVP)
&& (vp->v_type == VCHR)
&& fo_select(fp, flag[msk], wql_ptr, &context)) {
optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
n++;
}
nc++;
}
}
}
proc_fdunlock(p);
}
nc = 0;
if (kfcount != count) {
proc_fdlock(p);
for (msk = 0; msk < 3; msk++) {
iptr = (u_int32_t *)&ibits[msk * nw];
optr = (u_int32_t *)&obits[msk * nw];
for (i = 0; i < nfd; i += NFDBITS) {
bits = iptr[i/NFDBITS];
while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
bits &= ~(1 << j);
fp = fdp->fd_ofiles[fd];
if (fp == NULL ||
(fdp->fd_ofileflags[fd] & UF_RESERVED)) {
proc_fdunlock(p);
return(EBADF);
}
if (sel_pass == SEL_SECONDPASS) {
wql_ptr = (char *)0;
fp->f_flags &= ~FP_INSELECT;
fp->f_waddr = (void *)0;
} else {
wql_ptr = (wql + nc * SIZEOF_WAITQUEUE_LINK);
fp->f_flags |= FP_INSELECT;
fp->f_waddr = (void *)wqsub;
}
context.vc_ucred = fp->f_cred;
if ((fp->f_ops &&
((fp->f_type != DTYPE_VNODE)
|| (((vp = (struct vnode *)fp->f_data) != NULLVP)
&& (vp->v_type != VCHR))
)
&& fo_select(fp, flag[msk], wql_ptr, &context))) {
optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
n++;
}
nc++;
}
}
}
proc_fdunlock(p);
}
*retval = n;
return (0);
}
int poll_callback(struct kqueue *, struct kevent64_s *, void *);
struct poll_continue_args {
user_addr_t pca_fds;
u_int pca_nfds;
u_int pca_rfds;
};
int
poll(struct proc *p, struct poll_args *uap, int32_t *retval)
{
__pthread_testcancel(1);
return(poll_nocancel(p, (struct poll_nocancel_args *)uap, retval));
}
int
poll_nocancel(struct proc *p, struct poll_nocancel_args *uap, int32_t *retval)
{
struct poll_continue_args *cont;
struct pollfd *fds;
struct kqueue *kq;
struct timeval atv;
int ncoll, error = 0;
u_int nfds = uap->nfds;
u_int rfds = 0;
u_int i;
size_t ni;
if (nfds > OPEN_MAX ||
(nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && (proc_suser(p) || nfds > FD_SETSIZE)))
return (EINVAL);
kq = kqueue_alloc(p);
if (kq == NULL)
return (EAGAIN);
ni = nfds * sizeof(struct pollfd) + sizeof(struct poll_continue_args);
MALLOC(cont, struct poll_continue_args *, ni, M_TEMP, M_WAITOK);
if (NULL == cont) {
error = EAGAIN;
goto out;
}
fds = (struct pollfd *)&cont[1];
error = copyin(uap->fds, fds, nfds * sizeof(struct pollfd));
if (error)
goto out;
if (uap->timeout != -1) {
struct timeval rtv;
atv.tv_sec = uap->timeout / 1000;
atv.tv_usec = (uap->timeout % 1000) * 1000;
if (itimerfix(&atv)) {
error = EINVAL;
goto out;
}
getmicrouptime(&rtv);
timevaladd(&atv, &rtv);
} else {
atv.tv_sec = 0;
atv.tv_usec = 0;
}
ncoll = nselcoll;
OSBitOrAtomic(P_SELECT, &p->p_flag);
for (i = 0; i < nfds; i++) {
short events = fds[i].events;
struct kevent64_s kev;
int kerror = 0;
if (fds[i].fd < 0) {
fds[i].revents = 0;
continue;
}
kev.ident = fds[i].fd;
kev.flags = EV_ADD | EV_ONESHOT | EV_POLL;
kev.fflags = NOTE_LOWAT;
kev.data = 1;
kev.udata = CAST_USER_ADDR_T(&fds[i]);
kev.ext[0] = 0;
kev.ext[1] = 0;
if (events & ( POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND | POLLHUP )) {
kev.filter = EVFILT_READ;
if (!(events & ( POLLIN | POLLRDNORM )))
kev.flags |= EV_OOBAND;
kerror = kevent_register(kq, &kev, p);
}
if (kerror == 0 &&
events & ( POLLOUT | POLLWRNORM | POLLWRBAND )) {
kev.filter = EVFILT_WRITE;
kerror = kevent_register(kq, &kev, p);
}
if (kerror == 0 &&
events & ( POLLEXTEND | POLLATTRIB | POLLNLINK | POLLWRITE )) {
kev.filter = EVFILT_VNODE;
kev.fflags = 0;
if (events & POLLEXTEND)
kev.fflags |= NOTE_EXTEND;
if (events & POLLATTRIB)
kev.fflags |= NOTE_ATTRIB;
if (events & POLLNLINK)
kev.fflags |= NOTE_LINK;
if (events & POLLWRITE)
kev.fflags |= NOTE_WRITE;
kerror = kevent_register(kq, &kev, p);
}
if (kerror != 0) {
fds[i].revents = POLLNVAL;
rfds++;
} else
fds[i].revents = 0;
}
if (rfds > 0)
goto done;
cont->pca_fds = uap->fds;
cont->pca_nfds = nfds;
cont->pca_rfds = rfds;
error = kqueue_scan(kq, poll_callback, NULL, cont, &atv, p);
rfds = cont->pca_rfds;
done:
OSBitAndAtomic(~((uint32_t)P_SELECT), &p->p_flag);
if (error == ERESTART)
error = EINTR;
if (error == EWOULDBLOCK)
error = 0;
if (error == 0) {
error = copyout(fds, uap->fds, nfds * sizeof(struct pollfd));
*retval = rfds;
}
out:
if (NULL != cont)
FREE(cont, M_TEMP);
kqueue_dealloc(kq);
return (error);
}
int
poll_callback(__unused struct kqueue *kq, struct kevent64_s *kevp, void *data)
{
struct poll_continue_args *cont = (struct poll_continue_args *)data;
struct pollfd *fds = CAST_DOWN(struct pollfd *, kevp->udata);
short mask;
if (kevp->flags & EV_EOF)
fds->revents |= POLLHUP;
if (kevp->flags & EV_ERROR)
fds->revents |= POLLERR;
switch (kevp->filter) {
case EVFILT_READ:
if (fds->revents & POLLHUP)
mask = (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND );
else {
mask = 0;
if (kevp->data != 0)
mask |= (POLLIN | POLLRDNORM );
if (kevp->flags & EV_OOBAND)
mask |= ( POLLPRI | POLLRDBAND );
}
fds->revents |= (fds->events & mask);
break;
case EVFILT_WRITE:
if (!(fds->revents & POLLHUP))
fds->revents |= (fds->events & ( POLLOUT | POLLWRNORM | POLLWRBAND ));
break;
case EVFILT_VNODE:
if (kevp->fflags & NOTE_EXTEND)
fds->revents |= (fds->events & POLLEXTEND);
if (kevp->fflags & NOTE_ATTRIB)
fds->revents |= (fds->events & POLLATTRIB);
if (kevp->fflags & NOTE_LINK)
fds->revents |= (fds->events & POLLNLINK);
if (kevp->fflags & NOTE_WRITE)
fds->revents |= (fds->events & POLLWRITE);
break;
}
if (fds->revents)
cont->pca_rfds++;
return 0;
}
int
seltrue(__unused dev_t dev, __unused int flag, __unused struct proc *p)
{
return (1);
}
static int
selcount(struct proc *p, u_int32_t *ibits, __unused u_int32_t *obits,
int nfd, int *countp, int * kfcountp)
{
struct filedesc *fdp = p->p_fd;
int msk, i, j, fd;
u_int32_t bits;
struct fileproc *fp;
int n = 0;
u_int32_t *iptr;
u_int nw;
int error=0;
int kfc = 0;
int dropcount;
vnode_t vp;
if (fdp == NULL) {
*countp = 0;
*kfcountp = 0;
return(EIO);
}
nw = howmany(nfd, NFDBITS);
proc_fdlock(p);
for (msk = 0; msk < 3; msk++) {
iptr = (u_int32_t *)&ibits[msk * nw];
for (i = 0; i < nfd; i += NFDBITS) {
bits = iptr[i/NFDBITS];
while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
bits &= ~(1 << j);
fp = fdp->fd_ofiles[fd];
if (fp == NULL ||
(fdp->fd_ofileflags[fd] & UF_RESERVED)) {
*countp = 0;
*kfcountp = 0;
error = EBADF;
goto bad;
}
fp->f_iocount++;
if ((fp->f_type == DTYPE_VNODE)
&& ((vp = (struct vnode *)fp->f_data) != NULLVP)
&& (vp->v_type == VCHR) )
kfc++;
n++;
}
}
}
proc_fdunlock(p);
*countp = n;
*kfcountp = kfc;
return (0);
bad:
dropcount = 0;
if (n== 0)
goto out;
for (msk = 0; msk < 3; msk++) {
iptr = (u_int32_t *)&ibits[msk * nw];
for (i = 0; i < nfd; i += NFDBITS) {
bits = iptr[i/NFDBITS];
while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
bits &= ~(1 << j);
fp = fdp->fd_ofiles[fd];
if (dropcount >= n)
goto out;
fp->f_iocount--;
if (p->p_fpdrainwait && fp->f_iocount == 0) {
p->p_fpdrainwait = 0;
wakeup(&p->p_fpdrainwait);
}
dropcount++;
}
}
}
out:
proc_fdunlock(p);
return(error);
}
static int
seldrop(struct proc *p, u_int32_t *ibits, int nfd)
{
struct filedesc *fdp = p->p_fd;
int msk, i, j, fd;
u_int32_t bits;
struct fileproc *fp;
int n = 0;
u_int32_t *iptr;
u_int nw;
if (fdp == NULL) {
return(EIO);
}
nw = howmany(nfd, NFDBITS);
proc_fdlock(p);
for (msk = 0; msk < 3; msk++) {
iptr = (u_int32_t *)&ibits[msk * nw];
for (i = 0; i < nfd; i += NFDBITS) {
bits = iptr[i/NFDBITS];
while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
bits &= ~(1 << j);
fp = fdp->fd_ofiles[fd];
if (fp == NULL
#if 0
|| (fdp->fd_ofileflags[fd] & UF_RESERVED)
#endif
) {
proc_fdunlock(p);
return(EBADF);
}
n++;
fp->f_iocount--;
fp->f_flags &= ~FP_INSELECT;
if (p->p_fpdrainwait && fp->f_iocount == 0) {
p->p_fpdrainwait = 0;
wakeup(&p->p_fpdrainwait);
}
}
}
}
proc_fdunlock(p);
return (0);
}
void
selrecord(__unused struct proc *selector, struct selinfo *sip, void * p_wql)
{
thread_t cur_act = current_thread();
struct uthread * ut = get_bsdthread_info(cur_act);
if ((p_wql == (void *)0) && ((sip->si_flags & SI_INITED) == 0)) {
return;
}
if((p_wql == (void *)0)) {
return;
}
if ((sip->si_flags & SI_INITED) == 0) {
wait_queue_init(&sip->si_wait_queue, SYNC_POLICY_FIFO);
sip->si_flags |= SI_INITED;
sip->si_flags &= ~SI_CLEAR;
}
if (sip->si_flags & SI_RECORDED) {
sip->si_flags |= SI_COLL;
} else
sip->si_flags &= ~SI_COLL;
sip->si_flags |= SI_RECORDED;
if (!wait_queue_member(&sip->si_wait_queue, ut->uu_wqset))
wait_queue_link_noalloc(&sip->si_wait_queue, ut->uu_wqset,
(wait_queue_link_t)p_wql);
return;
}
void
selwakeup(struct selinfo *sip)
{
if ((sip->si_flags & SI_INITED) == 0) {
return;
}
if (sip->si_flags & SI_COLL) {
nselcoll++;
sip->si_flags &= ~SI_COLL;
#if 0
#endif
}
if (sip->si_flags & SI_RECORDED) {
wait_queue_wakeup_all(&sip->si_wait_queue, NULL, THREAD_AWAKENED);
sip->si_flags &= ~SI_RECORDED;
}
}
void
selthreadclear(struct selinfo *sip)
{
if ((sip->si_flags & SI_INITED) == 0) {
return;
}
if (sip->si_flags & SI_RECORDED) {
selwakeup(sip);
sip->si_flags &= ~(SI_RECORDED | SI_COLL);
}
sip->si_flags |= SI_CLEAR;
wait_queue_unlink_all(&sip->si_wait_queue);
}
#define DBG_POST 0x10
#define DBG_WATCH 0x11
#define DBG_WAIT 0x12
#define DBG_MOD 0x13
#define DBG_EWAKEUP 0x14
#define DBG_ENQUEUE 0x15
#define DBG_DEQUEUE 0x16
#define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
#define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
#define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
#define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
#define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
#define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
#define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
#define EVPROCDEQUE(p, evq) do { \
proc_lock(p); \
if (evq->ee_flags & EV_QUEUED) { \
TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
evq->ee_flags &= ~EV_QUEUED; \
} \
proc_unlock(p); \
} while (0);
void
evsofree(struct socket *sp)
{
struct eventqelt *evq, *next;
proc_t p;
if (sp == NULL)
return;
for (evq = sp->so_evlist.tqh_first; evq != NULL; evq = next) {
next = evq->ee_slist.tqe_next;
p = evq->ee_proc;
if (evq->ee_flags & EV_QUEUED) {
EVPROCDEQUE(p, evq);
}
TAILQ_REMOVE(&sp->so_evlist, evq, ee_slist); FREE(evq, M_TEMP);
}
}
void
evpipefree(struct pipe *cpipe)
{
struct eventqelt *evq, *next;
proc_t p;
for (evq = cpipe->pipe_evlist.tqh_first; evq != NULL; evq = next) {
next = evq->ee_slist.tqe_next;
p = evq->ee_proc;
EVPROCDEQUE(p, evq);
TAILQ_REMOVE(&cpipe->pipe_evlist, evq, ee_slist); FREE(evq, M_TEMP);
}
}
static void
evprocenque(struct eventqelt *evq)
{
proc_t p;
assert(evq);
p = evq->ee_proc;
KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, (uint32_t)evq, evq->ee_flags, evq->ee_eventmask,0,0);
proc_lock(p);
if (evq->ee_flags & EV_QUEUED) {
proc_unlock(p);
KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
return;
}
evq->ee_flags |= EV_QUEUED;
TAILQ_INSERT_TAIL(&p->p_evlist, evq, ee_plist);
proc_unlock(p);
wakeup(&p->p_evlist);
KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
}
void
postpipeevent(struct pipe *pipep, int event)
{
int mask;
struct eventqelt *evq;
if (pipep == NULL)
return;
KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, event,0,0,1,0);
for (evq = pipep->pipe_evlist.tqh_first;
evq != NULL; evq = evq->ee_slist.tqe_next) {
if (evq->ee_eventmask == 0)
continue;
mask = 0;
switch (event & (EV_RWBYTES | EV_RCLOSED | EV_WCLOSED)) {
case EV_RWBYTES:
if ((evq->ee_eventmask & EV_RE) && pipep->pipe_buffer.cnt) {
mask |= EV_RE;
evq->ee_req.er_rcnt = pipep->pipe_buffer.cnt;
}
if ((evq->ee_eventmask & EV_WR) &&
(pipep->pipe_buffer.size - pipep->pipe_buffer.cnt) >= PIPE_BUF) {
if (pipep->pipe_state & PIPE_EOF) {
mask |= EV_WR|EV_RESET;
break;
}
mask |= EV_WR;
evq->ee_req.er_wcnt = pipep->pipe_buffer.size - pipep->pipe_buffer.cnt;
}
break;
case EV_WCLOSED:
case EV_RCLOSED:
if ((evq->ee_eventmask & EV_RE)) {
mask |= EV_RE|EV_RCLOSED;
}
if ((evq->ee_eventmask & EV_WR)) {
mask |= EV_WR|EV_WCLOSED;
}
break;
default:
return;
}
if (mask) {
evq->ee_eventmask = 0;
evq->ee_req.er_eventbits |= mask;
KERNEL_DEBUG(DBG_MISC_POST, (uint32_t)evq, evq->ee_req.er_eventbits, mask, 1,0);
evprocenque(evq);
}
}
KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, 0,0,0,1,0);
}
#if SOCKETS
void
postevent(struct socket *sp, struct sockbuf *sb, int event)
{
int mask;
struct eventqelt *evq;
struct tcpcb *tp;
if (sb)
sp = sb->sb_so;
if (sp == NULL)
return;
KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, (int)sp, event, 0, 0, 0);
for (evq = sp->so_evlist.tqh_first;
evq != NULL; evq = evq->ee_slist.tqe_next) {
if (evq->ee_eventmask == 0)
continue;
mask = 0;
switch (event & EV_DMASK) {
case EV_OOB:
if ((evq->ee_eventmask & EV_EX)) {
if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK)))
mask |= EV_EX|EV_OOB;
}
break;
case EV_RWBYTES|EV_OOB:
if ((evq->ee_eventmask & EV_EX)) {
if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK)))
mask |= EV_EX|EV_OOB;
}
case EV_RWBYTES:
if ((evq->ee_eventmask & EV_RE) && soreadable(sp)) {
if (sp->so_error) {
if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
(tp->t_state == TCPS_CLOSED)) {
mask |= EV_RE|EV_RESET;
break;
}
}
}
mask |= EV_RE;
evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
if (sp->so_state & SS_CANTRCVMORE) {
mask |= EV_FIN;
break;
}
}
if ((evq->ee_eventmask & EV_WR) && sowriteable(sp)) {
if (sp->so_error) {
if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
(tp->t_state == TCPS_CLOSED)) {
mask |= EV_WR|EV_RESET;
break;
}
}
}
mask |= EV_WR;
evq->ee_req.er_wcnt = sbspace(&sp->so_snd);
}
break;
case EV_RCONN:
if ((evq->ee_eventmask & EV_RE)) {
mask |= EV_RE|EV_RCONN;
evq->ee_req.er_rcnt = sp->so_qlen + 1; }
break;
case EV_WCONN:
if ((evq->ee_eventmask & EV_WR)) {
mask |= EV_WR|EV_WCONN;
}
break;
case EV_RCLOSED:
if ((evq->ee_eventmask & EV_RE)) {
mask |= EV_RE|EV_RCLOSED;
}
break;
case EV_WCLOSED:
if ((evq->ee_eventmask & EV_WR)) {
mask |= EV_WR|EV_WCLOSED;
}
break;
case EV_FIN:
if (evq->ee_eventmask & EV_RE) {
mask |= EV_RE|EV_FIN;
}
break;
case EV_RESET:
case EV_TIMEOUT:
if (evq->ee_eventmask & EV_RE) {
mask |= EV_RE | event;
}
if (evq->ee_eventmask & EV_WR) {
mask |= EV_WR | event;
}
break;
default:
KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, -1, 0, 0, 0);
return;
}
KERNEL_DEBUG(DBG_MISC_POST, (int)evq, evq->ee_eventmask, evq->ee_req.er_eventbits, mask, 0);
if (mask) {
evq->ee_eventmask = 0;
evq->ee_req.er_eventbits |= mask;
evprocenque(evq);
}
}
KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, 0, 0, 0, 0);
}
#endif
int
watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
{
struct eventqelt *evq = (struct eventqelt *)0;
struct eventqelt *np = NULL;
struct eventreq64 *erp;
struct fileproc *fp = NULL;
int error;
KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_START, 0,0,0,0,0);
MALLOC(evq, struct eventqelt *, sizeof(struct eventqelt), M_TEMP, M_WAITOK);
if (evq == NULL)
return (ENOMEM);
erp = &evq->ee_req;
if (IS_64BIT_PROCESS(p)) {
error = copyin(uap->u_req, (caddr_t)erp, sizeof(struct eventreq64));
} else {
struct eventreq32 er32;
error = copyin(uap->u_req, (caddr_t)&er32, sizeof(struct eventreq32));
if (error == 0) {
erp->er_type = er32.er_type;
erp->er_handle = er32.er_handle;
erp->er_data = (user_addr_t)er32.er_data;
}
}
if (error) {
FREE(evq, M_TEMP);
KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
return(error);
}
KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,(uint32_t)evq,0,0);
error = 0;
proc_fdlock(p);
if (erp->er_type != EV_FD) {
error = EINVAL;
} else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
error = EBADF;
#if SOCKETS
} else if (fp->f_type == DTYPE_SOCKET) {
socket_lock((struct socket *)fp->f_data, 1);
np = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
#endif
} else if (fp->f_type == DTYPE_PIPE) {
PIPE_LOCK((struct pipe *)fp->f_data);
np = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
} else {
fp_drop(p, erp->er_handle, fp, 1);
error = EINVAL;
}
proc_fdunlock(p);
if (error) {
FREE(evq, M_TEMP);
KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
return(error);
}
for ( ; np != NULL; np = np->ee_slist.tqe_next) {
if (np->ee_proc == p) {
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET)
socket_unlock((struct socket *)fp->f_data, 1);
else
#endif
PIPE_UNLOCK((struct pipe *)fp->f_data);
fp_drop(p, erp->er_handle, fp, 0);
FREE(evq, M_TEMP);
KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
return(EINVAL);
}
}
erp->er_ecnt = erp->er_rcnt = erp->er_wcnt = erp->er_eventbits = 0;
evq->ee_proc = p;
evq->ee_eventmask = uap->u_eventmask & EV_MASK;
evq->ee_flags = 0;
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET) {
TAILQ_INSERT_TAIL(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
postevent((struct socket *)fp->f_data, 0, EV_RWBYTES);
socket_unlock((struct socket *)fp->f_data, 1);
} else
#endif
{
TAILQ_INSERT_TAIL(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
postpipeevent((struct pipe *)fp->f_data, EV_RWBYTES);
PIPE_UNLOCK((struct pipe *)fp->f_data);
}
fp_drop_event(p, erp->er_handle, fp);
KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, 0,0,0,0,0);
return(0);
}
int
waitevent(proc_t p, struct waitevent_args *uap, int *retval)
{
int error = 0;
struct eventqelt *evq;
struct eventreq64 *erp;
uint64_t abstime, interval;
boolean_t fast_poll = FALSE;
union {
struct eventreq64 er64;
struct eventreq32 er32;
} uer;
interval = 0;
if (uap->tv) {
struct timeval atv;
if (IS_64BIT_PROCESS(p)) {
if (uap->tv == (user_addr_t)-1)
fast_poll = TRUE;
} else if (uap->tv == (user_addr_t)((uint32_t)-1))
fast_poll = TRUE;
if (fast_poll == TRUE) {
if (p->p_evlist.tqh_first == NULL) {
KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_NONE, -1,0,0,0,0);
*retval = 1;
return (0);
}
proc_lock(p);
goto retry;
}
if (IS_64BIT_PROCESS(p)) {
struct user64_timeval atv64;
error = copyin(uap->tv, (caddr_t)&atv64, sizeof(atv64));
atv.tv_sec = atv64.tv_sec;
atv.tv_usec = atv64.tv_usec;
} else {
struct user32_timeval atv32;
error = copyin(uap->tv, (caddr_t)&atv32, sizeof(atv32));
atv.tv_sec = atv32.tv_sec;
atv.tv_usec = atv32.tv_usec;
}
if (error)
return(error);
if (itimerfix(&atv)) {
error = EINVAL;
return(error);
}
interval = tvtoabstime(&atv);
}
KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_START, 0,0,0,0,0);
proc_lock(p);
retry:
if ((evq = p->p_evlist.tqh_first) != NULL) {
erp = &evq->ee_req;
if (IS_64BIT_PROCESS(p))
bcopy((caddr_t)erp, (caddr_t)&uer.er64, sizeof (struct eventreq64));
else {
uer.er32.er_type = erp->er_type;
uer.er32.er_handle = erp->er_handle;
uer.er32.er_data = (uint32_t)erp->er_data;
uer.er32.er_ecnt = erp->er_ecnt;
uer.er32.er_rcnt = erp->er_rcnt;
uer.er32.er_wcnt = erp->er_wcnt;
uer.er32.er_eventbits = erp->er_eventbits;
}
TAILQ_REMOVE(&p->p_evlist, evq, ee_plist);
evq->ee_flags &= ~EV_QUEUED;
proc_unlock(p);
if (IS_64BIT_PROCESS(p))
error = copyout((caddr_t)&uer.er64, uap->u_req, sizeof(struct eventreq64));
else
error = copyout((caddr_t)&uer.er32, uap->u_req, sizeof(struct eventreq32));
KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,
evq->ee_req.er_handle,evq->ee_req.er_eventbits,(uint32_t)evq,0);
return (error);
}
else {
if (uap->tv && interval == 0) {
proc_unlock(p);
*retval = 1;
KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,0,0,0,0);
return (error);
}
if (interval != 0)
clock_absolutetime_interval_to_deadline(interval, &abstime);
else
abstime = 0;
KERNEL_DEBUG(DBG_MISC_WAIT, 1,(uint32_t)&p->p_evlist,0,0,0);
error = msleep1(&p->p_evlist, &p->p_mlock, (PSOCK | PCATCH), "waitevent", abstime);
KERNEL_DEBUG(DBG_MISC_WAIT, 2,(uint32_t)&p->p_evlist,0,0,0);
if (error == 0)
goto retry;
if (error == ERESTART)
error = EINTR;
if (error == EWOULDBLOCK) {
*retval = 1;
error = 0;
}
}
proc_unlock(p);
KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, 0,0,0,0,0);
return (error);
}
int
modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
{
struct eventreq64 er;
struct eventreq64 *erp = &er;
struct eventqelt *evq = NULL;
int error;
struct fileproc *fp;
int flag;
KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_START, 0,0,0,0,0);
if ((error = copyin(uap->u_req, (caddr_t)erp, sizeof(er.er_type) + sizeof(er.er_handle)))) {
KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
return(error);
}
proc_fdlock(p);
if (erp->er_type != EV_FD) {
error = EINVAL;
} else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
error = EBADF;
#if SOCKETS
} else if (fp->f_type == DTYPE_SOCKET) {
socket_lock((struct socket *)fp->f_data, 1);
evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
#endif
} else if (fp->f_type == DTYPE_PIPE) {
PIPE_LOCK((struct pipe *)fp->f_data);
evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
} else {
fp_drop(p, erp->er_handle, fp, 1);
error = EINVAL;
}
if (error) {
proc_fdunlock(p);
KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
return(error);
}
if ((uap->u_eventmask == EV_RM) && (fp->f_flags & FP_WAITEVENT)) {
fp->f_flags &= ~FP_WAITEVENT;
}
proc_fdunlock(p);
for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
if (evq->ee_proc == p)
break;
}
if (evq == NULL) {
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET)
socket_unlock((struct socket *)fp->f_data, 1);
else
#endif
PIPE_UNLOCK((struct pipe *)fp->f_data);
fp_drop(p, erp->er_handle, fp, 0);
KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, EINVAL,0,0,0,0);
return(EINVAL);
}
KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,(uint32_t)evq,0,0);
if (uap->u_eventmask == EV_RM) {
EVPROCDEQUE(p, evq);
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET) {
TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
socket_unlock((struct socket *)fp->f_data, 1);
} else
#endif
{
TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
PIPE_UNLOCK((struct pipe *)fp->f_data);
}
fp_drop(p, erp->er_handle, fp, 0);
FREE(evq, M_TEMP);
KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, 0,0,0,0,0);
return(0);
}
switch (uap->u_eventmask & EV_MASK) {
case 0:
flag = 0;
break;
case EV_RE:
case EV_WR:
case EV_RE|EV_WR:
flag = EV_RWBYTES;
break;
case EV_EX:
flag = EV_OOB;
break;
case EV_EX|EV_RE:
case EV_EX|EV_WR:
case EV_EX|EV_RE|EV_WR:
flag = EV_OOB|EV_RWBYTES;
break;
default:
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET)
socket_unlock((struct socket *)fp->f_data, 1);
else
#endif
PIPE_UNLOCK((struct pipe *)fp->f_data);
fp_drop(p, erp->er_handle, fp, 0);
KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
return(EINVAL);
}
if (evq->ee_flags & EV_QUEUED) {
EVPROCDEQUE(p, evq);
}
evq->ee_req.er_eventbits = 0;
evq->ee_eventmask = uap->u_eventmask & EV_MASK;
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET) {
postevent((struct socket *)fp->f_data, 0, flag);
socket_unlock((struct socket *)fp->f_data, 1);
} else
#endif
{
postpipeevent((struct pipe *)fp->f_data, flag);
PIPE_UNLOCK((struct pipe *)fp->f_data);
}
fp_drop(p, erp->er_handle, fp, 0);
KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,(uint32_t)fp->f_data,flag,0);
return(0);
}
int
waitevent_close(struct proc *p, struct fileproc *fp)
{
struct eventqelt *evq;
fp->f_flags &= ~FP_WAITEVENT;
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET) {
socket_lock((struct socket *)fp->f_data, 1);
evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
} else
#endif
if (fp->f_type == DTYPE_PIPE) {
PIPE_LOCK((struct pipe *)fp->f_data);
evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
}
else {
return(EINVAL);
}
proc_fdunlock(p);
for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
if (evq->ee_proc == p)
break;
}
if (evq == NULL) {
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET)
socket_unlock((struct socket *)fp->f_data, 1);
else
#endif
PIPE_UNLOCK((struct pipe *)fp->f_data);
proc_fdlock(p);
return(EINVAL);
}
EVPROCDEQUE(p, evq);
#if SOCKETS
if (fp->f_type == DTYPE_SOCKET) {
TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
socket_unlock((struct socket *)fp->f_data, 1);
} else
#endif
{
TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
PIPE_UNLOCK((struct pipe *)fp->f_data);
}
FREE(evq, M_TEMP);
proc_fdlock(p);
return(0);
}
int
gethostuuid(struct proc *p, struct gethostuuid_args *uap, __unused int32_t *retval)
{
kern_return_t kret;
int error;
mach_timespec_t mach_ts;
__darwin_uuid_t uuid_kern;
if ( proc_is64bit(p) ) {
struct user64_timespec ts;
error = copyin(uap->timeoutp, &ts, sizeof(ts));
if (error)
return (error);
mach_ts.tv_sec = ts.tv_sec;
mach_ts.tv_nsec = ts.tv_nsec;
} else {
struct user32_timespec ts;
error = copyin(uap->timeoutp, &ts, sizeof(ts) );
if (error)
return (error);
mach_ts.tv_sec = ts.tv_sec;
mach_ts.tv_nsec = ts.tv_nsec;
}
kret = IOBSDGetPlatformUUID(uuid_kern, mach_ts);
if (kret == KERN_SUCCESS) {
error = copyout(uuid_kern, uap->uuid_buf, sizeof(uuid_kern));
} else {
error = EWOULDBLOCK;
}
return (error);
}