2015-08-10 18:38:41 +02: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-10 18:41:58 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
2020-11-20 10:05:35 +01:00
|
|
|
#include <inttypes.h>
|
2015-08-10 18:38:41 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2015-08-10 18:41:58 +02:00
|
|
|
#include <debug.h>
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
#include <nuttx/clock.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2016-07-16 15:02:06 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
#include "inode/inode.h"
|
|
|
|
|
2020-08-20 19:37:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct epoll_head
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int occupied;
|
2020-12-23 03:45:10 +01:00
|
|
|
struct file fp;
|
|
|
|
struct inode in;
|
2020-08-20 19:37:17 +02:00
|
|
|
FAR epoll_data_t *data;
|
|
|
|
FAR struct pollfd *poll;
|
|
|
|
};
|
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
static int epoll_do_close(FAR struct file *filep);
|
|
|
|
static int epoll_do_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds, bool setup);
|
2020-12-23 03:45:10 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_epoll_ops =
|
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
.close = epoll_do_close,
|
|
|
|
.poll = epoll_do_poll
|
2020-12-23 03:45:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
static FAR struct epoll_head *epoll_head_from_fd(int fd)
|
2020-12-23 03:45:10 +01:00
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
FAR struct file *filep;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get file pointer by file descriptor */
|
|
|
|
|
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check fd come from us */
|
|
|
|
|
|
|
|
if (filep->f_inode->u.i_ops != &g_epoll_ops)
|
|
|
|
{
|
|
|
|
set_errno(EBADF);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (FAR struct epoll_head *)filep->f_inode->i_private;
|
2020-12-23 03:45:10 +01:00
|
|
|
}
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
static int epoll_do_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct epoll_head *eph = filep->f_inode->i_private;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
kmm_free(eph);
|
|
|
|
return OK;
|
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
static int epoll_do_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int epoll_do_create(int size, int flags)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2020-12-23 03:45:10 +01:00
|
|
|
FAR struct epoll_head *eph;
|
|
|
|
int reserve = size + 1;
|
2021-03-12 08:52:19 +01:00
|
|
|
int fd;
|
2020-12-23 03:45:10 +01:00
|
|
|
|
|
|
|
eph = (FAR struct epoll_head *)
|
|
|
|
kmm_zalloc(sizeof(struct epoll_head) +
|
|
|
|
sizeof(epoll_data_t) * reserve +
|
|
|
|
sizeof(struct pollfd) * reserve);
|
2021-03-12 08:52:19 +01:00
|
|
|
if (eph == NULL)
|
|
|
|
{
|
|
|
|
set_errno(ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
|
|
|
eph->size = size;
|
2020-08-20 19:37:17 +02:00
|
|
|
eph->data = (FAR epoll_data_t *)(eph + 1);
|
2020-12-23 03:45:10 +01:00
|
|
|
eph->poll = (FAR struct pollfd *)(eph->data + reserve);
|
|
|
|
|
|
|
|
INODE_SET_DRIVER(&eph->in);
|
|
|
|
eph->in.u.i_ops = &g_epoll_ops;
|
|
|
|
eph->fp.f_inode = &eph->in;
|
|
|
|
eph->in.i_private = eph;
|
|
|
|
|
|
|
|
eph->poll[0].ptr = &eph->fp;
|
|
|
|
eph->poll[0].events = POLLIN | POLLFILE;
|
2016-07-16 15:02:06 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
/* Alloc the file descriptor */
|
|
|
|
|
|
|
|
fd = files_allocate(&eph->in, flags, 0, eph, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
kmm_free(eph);
|
|
|
|
set_errno(-fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
return fd;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 04:58:27 +02:00
|
|
|
/****************************************************************************
|
2021-03-12 08:52:19 +01:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_create
|
2020-08-17 04:58:27 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
int epoll_create(int size)
|
2020-08-17 04:58:27 +02:00
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
return epoll_do_create(size, 0);
|
|
|
|
}
|
2020-08-17 04:58:27 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_create1
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2020-08-17 04:58:27 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
int epoll_create1(int flags)
|
|
|
|
{
|
|
|
|
return epoll_do_create(CONFIG_FS_NEPOLL_DESCRIPTORS, flags);
|
2020-08-17 04:58:27 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 18:38:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void epoll_close(int epfd)
|
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
close(epfd);
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_ctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
|
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
FAR struct epoll_head *eph;
|
2020-12-23 03:41:13 +01:00
|
|
|
int i;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
eph = epoll_head_from_fd(epfd);
|
|
|
|
if (eph == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-10 18:38:41 +02:00
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case EPOLL_CTL_ADD:
|
2020-11-20 10:05:35 +01:00
|
|
|
finfo("%08x CTL ADD(%d): fd=%d ev=%08" PRIx32 "\n",
|
2015-08-10 18:41:58 +02:00
|
|
|
epfd, eph->occupied, fd, ev->events);
|
2020-12-23 03:41:13 +01:00
|
|
|
if (eph->occupied >= eph->size)
|
|
|
|
{
|
|
|
|
set_errno(ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
for (i = 1; i <= eph->occupied; i++)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
|
|
|
if (eph->poll[i].fd == fd)
|
|
|
|
{
|
|
|
|
set_errno(EEXIST);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
eph->data[++eph->occupied] = ev->data;
|
2020-08-20 19:37:17 +02:00
|
|
|
eph->poll[eph->occupied].events = ev->events | POLLERR | POLLHUP;
|
2020-12-23 03:45:10 +01:00
|
|
|
eph->poll[eph->occupied].fd = fd;
|
2020-12-23 03:41:13 +01:00
|
|
|
|
|
|
|
break;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
|
|
|
case EPOLL_CTL_DEL:
|
2020-12-23 03:45:10 +01:00
|
|
|
for (i = 1; i <= eph->occupied; i++)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
|
|
|
if (eph->poll[i].fd == fd)
|
|
|
|
{
|
2021-03-12 08:52:55 +01:00
|
|
|
if (i != eph->occupied)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
|
|
|
memmove(&eph->data[i], &eph->data[i + 1],
|
|
|
|
sizeof(epoll_data_t) * (eph->occupied - i));
|
|
|
|
memmove(&eph->poll[i], &eph->poll[i + 1],
|
|
|
|
sizeof(struct pollfd) * (eph->occupied - i));
|
|
|
|
}
|
|
|
|
|
|
|
|
eph->occupied--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_errno(ENOENT);
|
|
|
|
return -1;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
|
|
|
case EPOLL_CTL_MOD:
|
2020-12-23 03:41:13 +01:00
|
|
|
finfo("%08x CTL MOD(%d): fd=%d ev=%08" PRIx32 "\n",
|
|
|
|
epfd, eph->occupied, fd, ev->events);
|
2020-12-23 03:45:10 +01:00
|
|
|
for (i = 1; i <= eph->occupied; i++)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
|
|
|
if (eph->poll[i].fd == fd)
|
|
|
|
{
|
|
|
|
eph->data[i] = ev->data;
|
|
|
|
eph->poll[i].events = ev->events | POLLERR | POLLHUP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_errno(ENOENT);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_errno(EINVAL);
|
|
|
|
return -1;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
if (eph->poll[0].sem)
|
|
|
|
{
|
|
|
|
eph->poll[0].revents |= eph->poll[0].events;
|
|
|
|
nxsem_post(eph->poll[0].sem);
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:41:13 +01:00
|
|
|
return 0;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-08-16 19:00:36 +02:00
|
|
|
* Name: epoll_pwait
|
2015-08-10 18:38:41 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-16 19:00:36 +02:00
|
|
|
int epoll_pwait(int epfd, FAR struct epoll_event *evs,
|
|
|
|
int maxevents, int timeout, FAR const sigset_t *sigmask)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
FAR struct epoll_head *eph;
|
2020-12-23 03:45:10 +01:00
|
|
|
struct timespec expire;
|
|
|
|
struct timespec curr;
|
|
|
|
struct timespec diff;
|
2017-07-22 16:52:14 +02:00
|
|
|
int counter;
|
2015-08-10 18:38:41 +02:00
|
|
|
int rc;
|
2016-07-16 15:02:06 +02:00
|
|
|
int i;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
eph = epoll_head_from_fd(epfd);
|
|
|
|
if (eph == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
if (timeout >= 0)
|
|
|
|
{
|
|
|
|
expire.tv_sec = timeout / 1000;
|
|
|
|
expire.tv_nsec = timeout % 1000 * 1000;
|
|
|
|
|
|
|
|
#ifdef CONFIG_CLOCK_MONOTONIC
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &curr);
|
|
|
|
#else
|
|
|
|
clock_gettime(CLOCK_REALTIME, &curr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
clock_timespec_add(&curr, &expire, &expire);
|
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
2020-08-16 19:00:36 +02:00
|
|
|
if (timeout < 0)
|
|
|
|
{
|
2020-12-23 03:45:10 +01:00
|
|
|
rc = ppoll(eph->poll, eph->occupied + 1, NULL, sigmask);
|
2020-08-16 19:00:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-23 03:45:10 +01:00
|
|
|
#ifdef CONFIG_CLOCK_MONOTONIC
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &curr);
|
|
|
|
#else
|
|
|
|
clock_gettime(CLOCK_REALTIME, &curr);
|
|
|
|
#endif
|
|
|
|
clock_timespec_subtract(&expire, &curr, &diff);
|
|
|
|
|
|
|
|
rc = ppoll(eph->poll, eph->occupied + 1, &diff, sigmask);
|
2020-08-16 19:00:36 +02:00
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
|
|
|
if (rc <= 0)
|
|
|
|
{
|
2020-12-23 03:45:10 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
else if (eph->poll[0].revents != 0)
|
|
|
|
{
|
|
|
|
if (--rc == 0)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2020-12-23 03:45:10 +01:00
|
|
|
goto again;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 22:05:24 +02:00
|
|
|
if (rc > maxevents)
|
|
|
|
{
|
|
|
|
rc = maxevents;
|
|
|
|
}
|
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
/* Iterate over non NULL event fds */
|
|
|
|
|
|
|
|
for (i = 0, counter = 1; i < rc && counter <= eph->occupied; counter++)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2020-08-20 19:37:17 +02:00
|
|
|
if (eph->poll[counter].revents != 0)
|
2017-07-22 16:52:14 +02:00
|
|
|
{
|
2020-08-20 19:37:17 +02:00
|
|
|
evs[i].data = eph->data[counter];
|
|
|
|
evs[i++].events = eph->poll[counter].revents;
|
|
|
|
if (eph->poll[counter].events & EPOLLONESHOT)
|
2020-08-16 19:32:21 +02:00
|
|
|
{
|
2020-08-20 19:37:17 +02:00
|
|
|
eph->poll[counter].events = 0; /* Disable oneshot internally */
|
2020-08-16 19:32:21 +02:00
|
|
|
}
|
2017-07-22 16:52:14 +02:00
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
2017-07-22 16:52:14 +02:00
|
|
|
return i;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
2020-08-16 19:00:36 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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);
|
|
|
|
}
|