vfs/epoll: Protect epoll_ctl by mutex
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
7231a5f044
commit
faf16fb810
@ -181,7 +181,7 @@ static int epoll_do_create(int size, int flags)
|
|||||||
if (eph == NULL)
|
if (eph == NULL)
|
||||||
{
|
{
|
||||||
set_errno(ENOMEM);
|
set_errno(ENOMEM);
|
||||||
return -1;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
nxmutex_init(&eph->lock);
|
nxmutex_init(&eph->lock);
|
||||||
@ -206,7 +206,7 @@ static int epoll_do_create(int size, int flags)
|
|||||||
nxmutex_destroy(&eph->lock);
|
nxmutex_destroy(&eph->lock);
|
||||||
kmm_free(eph);
|
kmm_free(eph);
|
||||||
set_errno(-fd);
|
set_errno(-fd);
|
||||||
return -1;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
@ -275,15 +275,22 @@ void epoll_close(int epfd)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
|
int epoll_ctl(int epfd, int op, int fd, FAR struct epoll_event *ev)
|
||||||
{
|
{
|
||||||
FAR struct epoll_head *eph;
|
FAR struct epoll_head *eph;
|
||||||
|
int ret;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
eph = epoll_head_from_fd(epfd);
|
eph = epoll_head_from_fd(epfd);
|
||||||
if (eph == NULL)
|
if (eph == NULL)
|
||||||
{
|
{
|
||||||
return -1;
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = nxmutex_lock(&eph->lock);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
goto err_without_lock;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (op)
|
switch (op)
|
||||||
@ -293,16 +300,16 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
|
|||||||
epfd, eph->occupied, fd, ev->events);
|
epfd, eph->occupied, fd, ev->events);
|
||||||
if (eph->occupied >= eph->size)
|
if (eph->occupied >= eph->size)
|
||||||
{
|
{
|
||||||
set_errno(ENOMEM);
|
ret = -ENOMEM;
|
||||||
return -1;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 1; i <= eph->occupied; i++)
|
for (i = 1; i <= eph->occupied; i++)
|
||||||
{
|
{
|
||||||
if (eph->poll[i].fd == fd)
|
if (eph->poll[i].fd == fd)
|
||||||
{
|
{
|
||||||
set_errno(EEXIST);
|
ret = -EEXIST;
|
||||||
return -1;
|
goto err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -331,8 +338,8 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
|
|||||||
|
|
||||||
if (i > eph->occupied)
|
if (i > eph->occupied)
|
||||||
{
|
{
|
||||||
set_errno(ENOENT);
|
ret = -ENOENT;
|
||||||
return -1;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
eph->occupied--;
|
eph->occupied--;
|
||||||
@ -353,19 +360,26 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
|
|||||||
|
|
||||||
if (i > eph->occupied)
|
if (i > eph->occupied)
|
||||||
{
|
{
|
||||||
set_errno(ENOENT);
|
ret = -ENOENT;
|
||||||
return -1;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
set_errno(EINVAL);
|
ret = -EINVAL;
|
||||||
return -1;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nxmutex_unlock(&eph->lock);
|
||||||
poll_notify(&eph->poll, 1, POLLIN);
|
poll_notify(&eph->poll, 1, POLLIN);
|
||||||
return 0;
|
return OK;
|
||||||
|
|
||||||
|
err:
|
||||||
|
nxmutex_unlock(&eph->lock);
|
||||||
|
err_without_lock:
|
||||||
|
set_errno(-ret);
|
||||||
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -386,7 +400,7 @@ int epoll_pwait(int epfd, FAR struct epoll_event *evs,
|
|||||||
eph = epoll_head_from_fd(epfd);
|
eph = epoll_head_from_fd(epfd);
|
||||||
if (eph == NULL)
|
if (eph == NULL)
|
||||||
{
|
{
|
||||||
return -1;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeout >= 0)
|
if (timeout >= 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user