Kevent bonus: epoll implementaion over kevent.

Evgeniy Polyakov
2007-01-10T12:21:13Z

As a usage scenario, compile-tested only.
Replace fs/eventpoll.c with this code and see,
how your kernel crashes. Or works.

:)

Signed-off-by: Evgeniy Polyakov <johnpol@2ka.mipt.ru>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/kevent.h>
#include <linux/ukevent.h>
#include <linux/syscalls.h>
#include <asm/poll.h>
#include <asm/uaccess.h>
#include <linux/eventpoll.h>

asmlinkage long sys_epoll_create(int size)
{
return sys_kevent_init(NULL, 0, 0);
}

#define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)

asmlinkage long sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
{
struct epoll_event epds;
struct ukevent uk;
int cmd, err;

switch (op) {
case EPOLL_CTL_DEL:
cmd = KEVENT_CTL_REMOVE;
break;
case EPOLL_CTL_ADD:
cmd = KEVENT_CTL_ADD;
break;
case EPOLL_CTL_MOD:
cmd = KEVENT_CTL_MODIFY;
break;
default:
err = -EINVAL;
goto err_out_exit;
}

if (copy_from_user(&epds, event, sizeof(struct epoll_event))) {
err = -EFAULT;
goto err_out_exit;
}

memset(&uk, 0, sizeof(struct ukevent));

uk.id.raw[0] = fd;
memcpy(uk.user, &epds.data, sizeof(u64));
uk.type = KEVENT_POLL;
uk.event = (epds.events & ~EP_PRIVATE_BITS)| POLLERR | POLLHUP;
uk.req_flags = KEVENT_REQ_ALWAYS_QUEUE | KEVENT_REQ_LAST_CHECK;
if (epds.events & EPOLLONESHOT)
uk.req_flags |= KEVENT_REQ_ONESHOT;
if (epds.events & EPOLLET)
uk.req_flags |= KEVENT_REQ_ET;

return sys_kevent_ctl(epfd, cmd, 1, &uk);

err_out_exit:
return err;
}

asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
int maxevents, int timeout)
{
struct ukevent uk[3];
struct timespec ts;
long num, i;
int err;

if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
err = -EFAULT;
goto err_out_exit;
}

memset(uk, 0, sizeof(uk));

ts.tv_sec = timeout/1000;
ts.tv_nsec = (timeout - ts.tv_sec*1000)*1000;

num = sys_kevent_get_events(epfd, 1, min(3, maxevents), ts, uk, 0);
if (num <= 0)
return num;

for (i=0; i<num; ++i) {
__u64 data;

memcpy(&data, &uk[i].user, sizeof(u64));

if (__put_user(uk[i].ret_data[0], &events[i].events) ||
__put_user(data, &events[i].data))
return -EFAULT;
}

return num;

err_out_exit:
return err;
}

--
Evgeniy Polyakov