nuttx/fs/vfs/fs_epoll.c
Xiang Xiao a354b9a9d1 fs/vfs: Implement EPOLLONESHOT flag
https://linux.die.net/man/2/epoll_ctl:
EPOLLONESHOT (since Linux 2.6.2)
Sets the one-shot behavior for the associated file descriptor.
This means that after an event is pulled out with epoll_wait(2)
the associated file descriptor is internally disabled and
no other events will be reported by the epoll interface.
The user must call epoll_ctl() with EPOLL_CTL_MOD to
rearm the file descriptor with a new event mask.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I6c0dc93e1cdae0e8cea5b487c7005de2da2c2ec3
2020-08-25 21:06:48 +01:00

294 lines
7.8 KiB
C

/****************************************************************************
* 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
****************************************************************************/
#include <nuttx/config.h>
#include <sys/epoll.h>
#include <stdint.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: epoll_create
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
****************************************************************************/
int epoll_create(int size)
{
FAR struct epoll_head *eph =
(FAR struct epoll_head *)kmm_malloc(sizeof(struct epoll_head));
eph->size = size;
eph->occupied = 0;
eph->evs = kmm_malloc(sizeof(struct epoll_event) * eph->size);
/* REVISIT: This will not work on machines where:
* sizeof(struct epoll_head *) > sizeof(int)
*/
return (int)((intptr_t)eph);
}
/****************************************************************************
* Name: epoll_create1
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
****************************************************************************/
int epoll_create1(int flags)
{
/* For current implementation, Close-on-exec is a default behavior,
* the handle of epoll(2) is not a real file handle.
*/
if (flags != EPOLL_CLOEXEC)
{
return EINVAL;
}
return epoll_create(CONFIG_FS_NEPOLL_DESCRIPTORS);
}
/****************************************************************************
* Name: epoll_close
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
****************************************************************************/
void epoll_close(int epfd)
{
/* 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);
kmm_free(eph->evs);
kmm_free(eph);
}
/****************************************************************************
* Name: epoll_ctl
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
****************************************************************************/
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
{
/* 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);
switch (op)
{
case EPOLL_CTL_ADD:
finfo("%08x CTL ADD(%d): fd=%d ev=%08x\n",
epfd, eph->occupied, fd, ev->events);
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;
finfo("%08x CTL MOD(%d): fd=%d ev=%08x\n",
epfd, eph->occupied, fd, ev->events);
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_pwait
****************************************************************************/
int epoll_pwait(int epfd, FAR struct epoll_event *evs,
int maxevents, int timeout, FAR const sigset_t *sigmask)
{
/* 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);
int counter;
int rc;
int i;
if (timeout < 0)
{
rc = ppoll((FAR struct pollfd *)eph->evs,
eph->occupied, NULL, sigmask);
}
else
{
struct timespec timeout_ts;
timeout_ts.tv_sec = timeout / 1000;
timeout_ts.tv_nsec = timeout % 1000 * 1000;
rc = ppoll((FAR struct pollfd *)eph->evs,
eph->occupied, &timeout_ts, sigmask);
}
if (rc <= 0)
{
if (rc < 0)
{
ferr("ERROR: %08x poll fail: %d for %d, %d msecs\n",
epfd, rc, eph->occupied, timeout);
for (i = 0; i < eph->occupied; i++)
{
ferr(" %02d: fd=%d\n", i, eph->evs[i].data.fd);
}
}
return rc;
}
/* Iterate over non NULL event fds */
if (rc > maxevents)
{
rc = maxevents;
}
for (i = 0, counter = 0; i < rc && counter < eph->occupied; counter++)
{
if (eph->evs[counter].revents != 0)
{
evs[i].data.fd = eph->evs[counter].data.fd;
evs[i].events = eph->evs[counter].revents;
if (eph->evs[counter].events & EPOLLONESHOT)
{
eph->evs[counter].events = 0; /* Disable oneshot internally */
}
i += 1;
}
}
return i;
}
/****************************************************************************
* Name: epoll_wait
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
****************************************************************************/
int epoll_wait(int epfd, FAR struct epoll_event *evs,
int maxevents, int timeout)
{
return epoll_pwait(epfd, evs, maxevents, timeout, NULL);
}