Skip to content

Commit

Permalink
main: reuse active flag for epoll mod handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Jan 30, 2023
1 parent a42c483 commit 4d34ec1
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,44 +314,39 @@ static int set_epoll_fds(struct re *re, struct fhs *fhs)

DEBUG_INFO("set_epoll_fds: fd=%d flags=0x%02x\n", fd, flags);

if (flags) {
event.data.ptr = fhs;
if (!flags) {
if (-1 == epoll_ctl(re->epfd, EPOLL_CTL_DEL, fd, &event)) {
err = errno;
DEBUG_INFO("epoll_ctl: EPOLL_CTL_DEL: fd=%d (%m)\n",
fd, err);
}
return err;
}

if (flags & FD_READ)
event.events |= EPOLLIN;
if (flags & FD_WRITE)
event.events |= EPOLLOUT;
if (flags & FD_EXCEPT)
event.events |= EPOLLERR;
event.data.ptr = fhs;

/* Try to add it first */
if (-1 == epoll_ctl(re->epfd, EPOLL_CTL_ADD, fd, &event)) {
if (flags & FD_READ)
event.events |= EPOLLIN;
if (flags & FD_WRITE)
event.events |= EPOLLOUT;
if (flags & FD_EXCEPT)
event.events |= EPOLLERR;

/* If already exist then modify it */
if (EEXIST == errno) {

if (-1 == epoll_ctl(re->epfd, EPOLL_CTL_MOD,
fd, &event)) {
err = errno;
DEBUG_WARNING("epoll_ctl:"
" EPOLL_CTL_MOD:"
" fd=%d (%m)\n",
fd, err);
}
}
else {
err = errno;
DEBUG_WARNING("epoll_ctl: EPOLL_CTL_ADD:"
" fd=%d (%m)\n",
fd, err);
}
if (fhs->active) {
if (-1 == epoll_ctl(re->epfd, EPOLL_CTL_MOD, fd, &event)) {
err = errno;
DEBUG_WARNING("epoll_ctl:"
" EPOLL_CTL_MOD:"
" fd=%d (%m)\n",
fd, err);
}
}
else {
if (-1 == epoll_ctl(re->epfd, EPOLL_CTL_DEL, fd, &event)) {
if (-1 == epoll_ctl(re->epfd, EPOLL_CTL_ADD, fd, &event)) {
err = errno;
DEBUG_INFO("epoll_ctl: EPOLL_CTL_DEL: fd=%d (%m)\n",
fd, err);
DEBUG_WARNING("epoll_ctl: EPOLL_CTL_ADD:"
" fd=%d (%m)\n",
fd, err);
}
}

Expand Down Expand Up @@ -606,7 +601,6 @@ static int fhs_update(struct re *re, struct fhs **fhsp, re_sock_t fd,
fhs->arg = arg;

if (!he) {
fhs->active = true;
++re->nfds;
hash_append(re->fhl, fhs_hash(re, fd), &fhs->le, fhs);
}
Expand Down Expand Up @@ -692,6 +686,13 @@ int fd_listen(re_sock_t fd, int flags, fd_h *fh, void *arg)
break;
}

if (err && flags) {
fd_close(fd);
DEBUG_WARNING("fd_listen: fd=%d flags=0x%02x (%m)\n", fd,
flags, err);
return err;
}

/* Stop listening */
if (!flags) {
fhs->active = false;
Expand All @@ -704,11 +705,8 @@ int fd_listen(re_sock_t fd, int flags, fd_h *fh, void *arg)
}
--re->nfds;
}

if (err && flags) {
fd_close(fd);
DEBUG_WARNING("fd_listen: fd=%d flags=0x%02x (%m)\n", fd,
flags, err);
else {
fhs->active = true;
}

return err;
Expand Down

0 comments on commit 4d34ec1

Please sign in to comment.