Skip to content
Myr

fdm: sync with latest FDM - fdm_del() now closes FD

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Daniel Eklöf · committed by Daniel Eklöf · parent 3767b29 · Browse files · 3 files+142 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+142 added−27 removed

  • other:+142 added−27 removed

3 files+142 added−27 removed

modified:fdm.c+136 added-23 removed
1#include "fdm.h"23#include <stdlib.h>4#include <stdbool.h>5#include <unistd.h>6#include <errno.h>7#include <assert.h>8#include <sys/epoll.h>
2 unmodified lines
11#define LOG_ENABLE_DBG 012#include "log.h"13#include "tllist.h"1414struct fd {15struct handler {16 int fd;16 void *data;17 fdm_handler_t handler;17 int events;18 fdm_handler_t callback;19 void *callback_data;20 bool deleted;21};2223struct fdm {24 int epoll_fd;25 bool is_polling;22 tll(struct fd) fds;26 tll(struct handler *) fds;27 tll(struct handler *) deferred_delete;28};2930struct fdm *31fdm_init(void)
6 unmodified lines
3839 struct fdm *fdm = malloc(sizeof(*fdm));40 *fdm = (struct fdm){41 .epoll_fd = epoll_fd,42 .is_polling = false,43 .fds = tll_init(),44 .deferred_delete = tll_init(),45 };46 return fdm;47}48
6 unmodified lines
55 if (tll_length(fdm->fds) > 0)56 LOG_WARN("FD list not empty");5758 assert(tll_length(fdm->fds) == 0);59 assert(tll_length(fdm->deferred_delete) == 0);6061 tll_free(fdm->fds);62 tll_free(fdm->deferred_delete);63 close(fdm->epoll_fd);64 free(fdm);65}66
1 unmodified line
68fdm_add(struct fdm *fdm, int fd, int events, fdm_handler_t handler, void *data)69{70#if defined(_DEBUG)71 tll_foreach(fdm->fds, it) {63 if (it->item.fd == fd) {72 if (it->item->fd == fd) {73 LOG_ERR("FD=%d already registered", fd);74 return false;75 }76 }77#endif7870 tll_push_back(71 fdm->fds, ((struct fd){.fd = fd, .data = data, .handler = handler}));79 struct handler *fd_data = malloc(sizeof(*fd_data));80 *fd_data = (struct handler) {81 .fd = fd,82 .events = events,83 .callback = handler,84 .callback_data = data,85 .deleted = false,86 };8788 tll_push_back(fdm->fds, fd_data);8990 struct epoll_event ev = {91 .events = events,75 .data = {.ptr = &tll_back(fdm->fds)},92 .data = {.ptr = fd_data},93 };9495 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {79 LOG_ERRNO("failed to register FD with epoll");96 LOG_ERRNO("failed to register FD=%d with epoll", fd);97 free(fd_data);98 tll_pop_back(fdm->fds);99 return false;100 }101102 return true;103}104105static bool106fdm_del_internal(struct fdm *fdm, int fd, bool close_fd)107{108 if (fd == -1)109 return true;110111 tll_foreach(fdm->fds, it) {112 if (it->item->fd != fd)113 continue;114115 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_DEL, fd, NULL) < 0)116 LOG_ERRNO("failed to unregister FD=%d from epoll", fd);117118 if (close_fd)119 close(it->item->fd);120121 it->item->deleted = true;122 if (fdm->is_polling)123 tll_push_back(fdm->deferred_delete, it->item);124 else125 free(it->item);126127 tll_remove(fdm->fds, it);128 return true;129 }130131 LOG_ERR("no such FD: %d", fd);132 return false;133}134135bool136fdm_del(struct fdm *fdm, int fd)137{138 return fdm_del_internal(fdm, fd, true);139}140141bool142fdm_del_no_close(struct fdm *fdm, int fd)143{144 return fdm_del_internal(fdm, fd, false);145}146147static bool148event_modify(struct fdm *fdm, struct handler *fd, int new_events)149{150 if (new_events == fd->events)151 return true;152153 struct epoll_event ev = {154 .events = new_events,155 .data = {.ptr = fd},156 };157158 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_MOD, fd->fd, &ev) < 0) {159 LOG_ERRNO("failed to modify FD=%d with epoll (events 0x%08x -> 0x%08x)",160 fd->fd, fd->events, new_events);161 return false;162 }163164 fd->events = new_events;165 return true;166}167168bool169fdm_event_add(struct fdm *fdm, int fd, int events)170{171 tll_foreach(fdm->fds, it) {91 if (it->item.fd == fd) {92 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_DEL, fd, NULL) < 0)93 LOG_ERRNO("failed to unregister FD=%d from epoll", fd);172 if (it->item->fd != fd)173 continue;17495 tll_remove(fdm->fds, it);96 return true;97 }175 return event_modify(fdm, it->item, it->item->events | events);176 }177100 LOG_ERR("no such FD: %d", fd);178 LOG_ERR("FD=%d not registered with the FDM", fd);179 return false;180}181182bool183fdm_event_del(struct fdm *fdm, int fd, int events)184{185 tll_foreach(fdm->fds, it) {186 if (it->item->fd != fd)187 continue;188189 return event_modify(fdm, it->item, it->item->events & ~events);190 }191192 LOG_ERR("FD=%d not registered with the FDM", fd);193 return false;194}195196bool197fdm_poll(struct fdm *fdm)198{199 assert(!fdm->is_polling && "nested calls to fdm_poll() not allowed");200 if (fdm->is_polling) {201 LOG_ERR("nested calls to fdm_poll() not allowed");202 return false;203 }204205 struct epoll_event events[tll_length(fdm->fds)];206108 int ret = epoll_wait(fdm->epoll_fd, events, tll_length(fdm->fds), -1);109 if (ret == -1) {207 int r = epoll_wait(fdm->epoll_fd, events, tll_length(fdm->fds), -1);208 if (r == -1) {209 if (errno == EINTR)210 return true;211212 LOG_ERRNO("failed to epoll");213 return false;214 }215117 for (int i = 0; i < ret; i++) {118 struct fd *fd = events[i].data.ptr;119 if (!fd->handler(fdm, fd->fd, events[i].events, fd->data))120 return false;216 bool ret = true;217218 fdm->is_polling = true;219 for (int i = 0; i < r; i++) {220 struct handler *fd = events[i].data.ptr;221 if (fd->deleted)222 continue;223224 if (!fd->callback(fdm, fd->fd, events[i].events, fd->callback_data)) {225 ret = false;226 break;227 }228 }229 fdm->is_polling = false;230231 tll_foreach(fdm->deferred_delete, it) {232 free(it->item);233 tll_remove(fdm->deferred_delete, it);234 }235123 return true;236 return ret;237}
modified:fdm.h+4 added
9 unmodified lines
10void fdm_destroy(struct fdm *fdm);1112bool fdm_add(struct fdm *fdm, int fd, int events, fdm_handler_t handler, void *data);13bool fdm_del(struct fdm *fdm, int fd);14bool fdm_del_no_close(struct fdm *fdm, int fd);1516bool fdm_event_add(struct fdm *fdm, int fd, int events);17bool fdm_event_del(struct fdm *fdm, int fd, int events);1819bool fdm_poll(struct fdm *fdm);
modified:wayland.c+2 added-4 removed
889 unmodified lines
890 if (wayl == NULL)891 return;892893 if (wayl->display != NULL)894 fdm_del(wayl->fdm, wl_display_get_fd(wayl->display));894 fdm_del_no_close(wayl->fdm, wl_display_get_fd(wayl->display));895896 if (wayl->repeat.fd > 0) {896 if (wayl->repeat.fd > 0)897 fdm_del(wayl->fdm, wayl->repeat.fd);898 close(wayl->repeat.fd);899 }898899 tll_foreach(wayl->monitors, it) {900 free(it->item.name);901 if (it->item.xdg)
54 unmodified lines