edge-epoll.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* shttpd - install and delete edge triggers (AKA epoll on Linux)
  2. Copyright (C) 2018 Ariadne Devos
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <sHT/bugs.h>
  14. #include <sHT/compiler.h>
  15. #include <sHT/scheduling.h>
  16. #include "worker.h"
  17. #include "fd.h"
  18. #include <errno.h>
  19. #include <stddef.h>
  20. #include <sys/epoll.h>
  21. int
  22. sHT_alloc_watch_set(void)
  23. {
  24. int fd = epoll_create1(EPOLL_CLOEXEC);
  25. if (sHT_likely(sHT_test_hidden(fd, fd >= 0)))
  26. /* Invalid values may speculatively be returned. */
  27. return fd;
  28. /* Invalid values may speculatively be returned. */
  29. int err = errno;
  30. switch (err) {
  31. case EMFILE:
  32. sHT_hide_var(err);
  33. return sHT_WATCH_SET_ALLOC_EMFILE;
  34. case ENFILE:
  35. sHT_hide_var(err);
  36. return sHT_WATCH_SET_ALLOC_ENFILE;
  37. case ENOMEM:
  38. sHT_hide_var(err);
  39. return sHT_WATCH_SET_ALLOC_ENOMEM;
  40. default:
  41. sHT_hide_var(err);
  42. /* If the kernel is acting strangely, report this.
  43. If we are on a speculative execution -- there is a bug
  44. in the kernel, worry about *that* instead. */
  45. sHT_halt("alloc_event_polling: errno valid?");
  46. /* Everything may be a speculative placeholder! */
  47. return 0;
  48. }
  49. }
  50. void
  51. sHT_free_watch_set(int watches)
  52. {
  53. sHT_close(watches);
  54. }
  55. /* When using /dev/kqueue is supported, rename this file to worker/edge-epoll.c
  56. Looking at FreeBSD's man page on that
  57. */
  58. /* ... except for EPOLLONESHOT, EPOLLWAKEUP and EPOLLEXCLUSIVE.
  59. Note EPOLLET, which lets polling be edge-triggered instead of
  60. level-triggered.
  61. */
  62. #define sHT_EPOLL_ALL (EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLPRI | EPOLLERR | \
  63. EPOLLHUP | EPOLLET)
  64. /* TODO: perhaps a string table (see Ulrich Drepper's optimisation tutorial) */
  65. static const struct sHT_bug_option sHT_epoll_ctl_bugs[] =
  66. { { .code = EBADFD, .msg = "not a fd" },
  67. { .code = EEXIST, .msg = "only one trigger at the time" },
  68. { .code = EINVAL, .msg = "bad fd type for trigger" },
  69. { .code = EPERM, .msg = "bad fd type for trigger" },
  70. { .code = ENOENT, .msg = "fd has no trigger" },
  71. { .code = ELOOP, .msg = "bad fd type for trigger" },
  72. { .code = 0, .msg = "worker/egde-epoll.c" } };
  73. enum sHT_watch_set_install
  74. sHT_install_edge_trigger(sHT_watch_set watches, int fd, struct sHT_task *task)
  75. {
  76. struct epoll_event e = {
  77. .events = sHT_EPOLL_ALL,
  78. .data = { .ptr = task },
  79. };
  80. int ret = epoll_ctl(watches, EPOLL_CTL_ADD, fd, &e);
  81. if (sHT_likely(sHT_test_hidden(ret, ret == 0)))
  82. return 0;
  83. int err = errno;
  84. switch (err) {
  85. case ENOSPC:
  86. sHT_hide_var(err);
  87. return sHT_WATCH_SET_INSTALL_LIMIT;
  88. case ENOMEM:
  89. sHT_hide_var(err);
  90. return sHT_WATCH_SET_INSTALL_ENOMEM;
  91. default:
  92. sHT_hide_var(err);
  93. sHT_bug(sHT_epoll_ctl_bugs, err);
  94. /* Everything may be a speculative placeholder! */
  95. return 0;
  96. }
  97. }
  98. void
  99. sHT_delete_edge_trigger(sHT_watch_set watches, int fd)
  100. {
  101. /* TODO: I remember reading the kernel source, looking for memory
  102. allocations. I don't think there were any in case of
  103. EPOLL_CTL_DELETE, do the analysis again and refine the man page.
  104. I assume this cannot happen. */
  105. /* TODO: NULL is not portable to Linux < 2.6.9. */
  106. int ret = epoll_ctl(watches, EPOLL_CTL_DEL, fd, NULL);
  107. if (sHT_likely(sHT_test_hidden(ret, ret == 0)))
  108. return;
  109. sHT_bug(sHT_epoll_ctl_bugs, errno);
  110. }