fixed mux.c to prevent an epoll_wait() error when no file descriptor is inserted

This commit is contained in:
hyung-hwan 2019-08-22 15:51:33 +00:00
parent cd14f62522
commit 8fc780bacd

View File

@ -386,6 +386,7 @@ void qse_mux_fini (qse_mux_t* mux)
QSE_MMGR_FREE (mux->mmgr, mux->me.ptr);
mux->me.ubound = 0;
mux->me.ptr = QSE_NULL;
mux->maxhnd = -1;
}
@ -404,6 +405,7 @@ void qse_mux_fini (qse_mux_t* mux)
QSE_MMGR_FREE (mux->mmgr, mux->me.ptr);
mux->me.ubound = 0;
mux->me.ptr = QSE_NULL;
}
#elif defined(USE_EPOLL)
@ -414,6 +416,7 @@ void qse_mux_fini (qse_mux_t* mux)
QSE_MMGR_FREE (mux->mmgr, mux->ee.ptr);
mux->ee.len = 0;
mux->ee.capa = 0;
mux->ee.ptr = QSE_NULL;
}
if (mux->me.ptr)
@ -422,12 +425,12 @@ void qse_mux_fini (qse_mux_t* mux)
for (i = 0; i < mux->me.ubound; i++)
{
if (mux->me.ptr[i])
QSE_MMGR_FREE (mux->mmgr, mux->me.ptr[i]);
if (mux->me.ptr[i]) QSE_MMGR_FREE (mux->mmgr, mux->me.ptr[i]);
}
QSE_MMGR_FREE (mux->mmgr, mux->me.ptr);
mux->me.ubound = 0;
mux->me.ptr = QSE_NULL;
}
#elif defined(__OS2__)
@ -443,9 +446,14 @@ void qse_mux_fini (qse_mux_t* mux)
QSE_MMGR_FREE (mux->mmgr, mux->me.ptr);
mux->me.ubound = 0;
mux->me.ptr = QSE_NULL;
}
if (mux->fdarr) QSE_MMGR_FREE (mux->mmgr, mux->fdarr);
if (mux->fdarr)
{
QSE_MMGR_FREE (mux->mmgr, mux->fdarr);
mux->fdarr = QSE_NULL;
}
#endif
}
@ -480,7 +488,7 @@ int qse_mux_insert (qse_mux_t* mux, const qse_mux_evt_t* evt)
#endif
/* sanity check */
if (!(evt->mask & (QSE_MUX_IN | QSE_MUX_OUT)) || evt->hnd < 0)
if (evt && (!(evt->mask & (QSE_MUX_IN | QSE_MUX_OUT)) || evt->hnd < 0))
{
mux->errnum = QSE_MUX_EINVAL;
return -1;
@ -535,15 +543,13 @@ int qse_mux_insert (qse_mux_t* mux, const qse_mux_evt_t* evt)
if (evt->mask & QSE_MUX_IN)
{
EV_SET (&chlist[count], evt->hnd,
EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
EV_SET (&chlist[count], evt->hnd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
count++;
}
if (evt->mask & QSE_MUX_OUT)
{
EV_SET (&chlist[count], evt->hnd,
EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, 0);
EV_SET (&chlist[count], evt->hnd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, 0);
count++;
}
@ -639,9 +645,7 @@ int qse_mux_insert (qse_mux_t* mux, const qse_mux_evt_t* evt)
newcapa = (mux->ee.capa + 1) * 2;
newcapa = QSE_ALIGNTO_POW2(newcapa, 256);
tmp = QSE_MMGR_REALLOC (
mux->mmgr, mux->ee.ptr,
QSE_SIZEOF(*mux->ee.ptr) * newcapa);
tmp = QSE_MMGR_REALLOC(mux->mmgr, mux->ee.ptr, QSE_SIZEOF(*mux->ee.ptr) * newcapa);
if (tmp == QSE_NULL)
{
mux->errnum = QSE_MUX_ENOMEM;
@ -954,17 +958,36 @@ int qse_mux_poll (qse_mux_t* mux, const qse_ntime_t* tmout)
#elif defined(USE_EPOLL)
int nfds, i;
qse_mux_evt_t* evt, xevt;
int epoll_tmout;
nfds = epoll_wait(
mux->fd, mux->ee.ptr, mux->ee.len,
(tmout? QSE_SECNSEC_TO_MSEC(tmout->sec,tmout->nsec): -1)
);
epoll_tmout = tmout? QSE_SECNSEC_TO_MSEC(tmout->sec,tmout->nsec): -1;
if (mux->ee.len <= 0)
{
/* epoll_wait() requires the third parameter to be greater than 0.
* so let me use a dummy variable to prevent an EINVAL error by epoll_wait() */
struct epoll_event dummy;
nfds = epoll_wait(mux->fd, &dummy, 1, epoll_tmout);
if (nfds <= -1)
{
mux->errnum = skerr_to_errnum(errno);
return -1;
}
/* nfds is supposed to be 0 as no file descriptors are watched. */
nfds = 0; /* but reset this to 0 just in case. */
}
else
{
nfds = epoll_wait(mux->fd, mux->ee.ptr, mux->ee.len, epoll_tmout);
if (nfds <= -1)
{
mux->errnum = skerr_to_errnum(errno);
return -1;
}
}
for (i = 0; i < nfds; i++)
{
/*int hnd = mux->ee.ptr[i].data.fd;