tdz/platform_system_libfdio — pull request #1 by jhector (2014-07-02 02:29:14+00:00)
Title: Bug 1031122: Cleanup |fd_state| entry after fd removal from epoll

This fixes the problem where the library tries to modify an file descriptor event that doesn't exist.

Let's we want to watch fd=5, our fd_state array looks like this:
...
fd_state[5].event.events == 0
...
we call **add_fd_to_epoll_loop()** to add a callback for the event. The function checks if epoll is already watching this file descriptor (enabled = !!fd_state[5].event.events). If it does, it calls **epoll_ctl()** with **EPOLL_CTL_MOD**, if it doesn't **EPOLL_CTL_ADD** will be used. After this, fd_state looks like this:
...
fd_state[5].event.events == EPOLLIN
...
After we ware done with fd 5 we call **remove_fd_from_epoll_loop()** to let epoll know it shouldn't watch fd 5 anymore. **EPOLL_CTL_DEL** is used to remove the file descriptor.

The next time we want to add file descriptor 5 to epoll, the fd_state has still the **EPOLLIN** entry for fd = 5 (since it hasn't been removed). This leads to the use of **EPOLL_CTL_MOD**, because the library thinks it has already added it.

At this point **epoll_ctl()** will fail, which also makes **add_fd_to_epoll_loop()** fail.