2015-08-11 00:38:41 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/vfs/fs_epoll.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Anton D. Kachalov. All rights reserved.
|
|
|
|
* Author: Anton D. Kachalov <mouse@mayc.ru>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-11 00:41:58 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
2015-08-11 00:38:41 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2015-08-11 00:41:58 +08:00
|
|
|
#include <debug.h>
|
2015-08-11 00:38:41 +08:00
|
|
|
|
2016-07-16 21:02:06 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2015-08-11 00:38:41 +08:00
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_create
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int epoll_create(int size)
|
|
|
|
{
|
|
|
|
FAR struct epoll_head *eph =
|
2016-07-16 21:02:06 +08:00
|
|
|
(FAR struct epoll_head *)kmm_malloc(sizeof(struct epoll_head));
|
2015-08-11 00:38:41 +08:00
|
|
|
|
|
|
|
eph->size = size;
|
|
|
|
eph->occupied = 0;
|
2016-07-16 21:02:06 +08:00
|
|
|
eph->evs = kmm_malloc(sizeof(struct epoll_event) * eph->size);
|
|
|
|
|
|
|
|
/* REVISIT: This will not work on machines where:
|
|
|
|
* sizeof(struct epoll_head *) > sizeof(int)
|
|
|
|
*/
|
2015-08-11 00:38:41 +08:00
|
|
|
|
2016-07-16 21:02:06 +08:00
|
|
|
return (int)((intptr_t)eph);
|
2015-08-11 00:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void epoll_close(int epfd)
|
|
|
|
{
|
2016-07-16 21:02:06 +08:00
|
|
|
/* REVISIT: This will not work on machines where:
|
|
|
|
* sizeof(struct epoll_head *) > sizeof(int)
|
|
|
|
*/
|
2015-08-11 00:38:41 +08:00
|
|
|
|
2016-07-16 21:02:06 +08:00
|
|
|
FAR struct epoll_head *eph = (FAR struct epoll_head *)((intptr_t)epfd);
|
|
|
|
|
|
|
|
kmm_free(eph->evs);
|
|
|
|
kmm_free(eph);
|
2015-08-11 00:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_ctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
|
|
|
|
{
|
2016-07-16 21:02:06 +08:00
|
|
|
/* REVISIT: This will not work on machines where:
|
|
|
|
* sizeof(struct epoll_head *) > sizeof(int)
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct epoll_head *eph = (FAR struct epoll_head *)((intptr_t)epfd);
|
2015-08-11 00:38:41 +08:00
|
|
|
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case EPOLL_CTL_ADD:
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("%08x CTL ADD(%d): fd=%d ev=%08x\n",
|
2015-08-11 00:41:58 +08:00
|
|
|
epfd, eph->occupied, fd, ev->events);
|
2015-08-11 00:38:41 +08:00
|
|
|
|
|
|
|
eph->evs[eph->occupied].events = ev->events | POLLERR | POLLHUP;
|
|
|
|
eph->evs[eph->occupied++].data.fd = fd;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case EPOLL_CTL_DEL:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < eph->occupied; i++)
|
|
|
|
{
|
|
|
|
if (eph->evs[i].data.fd == fd)
|
|
|
|
{
|
|
|
|
if (i != eph->occupied-1)
|
|
|
|
{
|
|
|
|
memmove(&eph->evs[i], &eph->evs[i + 1],
|
|
|
|
eph->occupied - i);
|
|
|
|
}
|
|
|
|
|
|
|
|
eph->occupied--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EPOLL_CTL_MOD:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("%08x CTL MOD(%d): fd=%d ev=%08x\n",
|
2015-08-11 00:41:58 +08:00
|
|
|
epfd, eph->occupied, fd, ev->events);
|
2015-08-11 00:38:41 +08:00
|
|
|
|
|
|
|
for (i = 0; i < eph->occupied; i++)
|
|
|
|
{
|
|
|
|
if (eph->evs[i].data.fd == fd)
|
|
|
|
{
|
|
|
|
eph->evs[i].events = ev->events | POLLERR | POLLHUP;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_wait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int epoll_wait(int epfd, FAR struct epoll_event *evs, int maxevents,
|
|
|
|
int timeout)
|
|
|
|
{
|
2016-07-16 21:02:06 +08:00
|
|
|
/* REVISIT: This will not work on machines where:
|
|
|
|
* sizeof(struct epoll_head *) > sizeof(int)
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct epoll_head *eph = (FAR struct epoll_head *)((intptr_t)epfd);
|
2015-08-11 00:38:41 +08:00
|
|
|
int rc;
|
2016-07-16 21:02:06 +08:00
|
|
|
int i;
|
2015-08-11 00:38:41 +08:00
|
|
|
|
|
|
|
rc = poll((FAR struct pollfd *)eph->evs, eph->occupied, timeout);
|
|
|
|
|
|
|
|
if (rc <= 0)
|
|
|
|
{
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
2016-06-12 07:14:02 +08:00
|
|
|
ferr("ERROR: %08x poll fail: %d for %d, %d msecs\n",
|
2015-08-11 00:41:58 +08:00
|
|
|
epfd, rc, eph->occupied, timeout);
|
2015-08-11 00:38:41 +08:00
|
|
|
|
|
|
|
for (i = 0; i < eph->occupied; i++)
|
|
|
|
{
|
2016-06-12 07:14:02 +08:00
|
|
|
ferr(" %02d: fd=%d\n", i, eph->evs[i].data.fd);
|
2015-08-11 00:38:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < rc; i++)
|
|
|
|
{
|
|
|
|
evs[i].data.fd = (pollevent_t)eph->evs[i].data.fd;
|
|
|
|
evs[i].events = (pollevent_t)eph->evs[i].revents;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_POLL */
|