shim_epoll.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_epoll.c
  17. *
  18. * Implementation of system call "epoll_create", "epoll_create1", "epoll_ctl"
  19. * and "epoll_wait".
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_table.h>
  23. #include <shim_thread.h>
  24. #include <shim_handle.h>
  25. #include <shim_fs.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <errno.h>
  29. #include <linux/eventpoll.h>
  30. #define EPOLLIN 0x001
  31. #define EPOLLPRI 0x002
  32. #define EPOLLOUT 0x004
  33. #define EPOLLRDNORM 0x040
  34. #define EPOLLRDBAND 0x080
  35. #define EPOLLWRNORM 0x100
  36. #define EPOLLERBAND 0x200
  37. #define EPOLLERR 0x008
  38. #define EPOLLHUP 0x010
  39. #define EPOLLRDHUP 0x2000
  40. #define MAX_EPOLL_FDS 1024
  41. struct shim_mount epoll_builtin_fs;
  42. struct shim_epoll_fd {
  43. FDTYPE fd;
  44. unsigned int events;
  45. __u64 data;
  46. unsigned int revents;
  47. struct shim_handle * handle;
  48. PAL_HANDLE pal_handle;
  49. struct list_head list;
  50. };
  51. int shim_do_epoll_create1 (int flags)
  52. {
  53. struct shim_handle * hdl = get_new_handle();
  54. if (!hdl)
  55. return -ENOMEM;
  56. struct shim_epoll_handle * epoll = &hdl->info.epoll;
  57. hdl->type = TYPE_EPOLL;
  58. set_handle_fs(hdl, &epoll_builtin_fs);
  59. epoll->maxfds = MAX_EPOLL_FDS;
  60. epoll->nfds = 0;
  61. epoll->pal_fds = malloc(sizeof(FDTYPE) * MAX_EPOLL_FDS);
  62. epoll->pal_handles = malloc(sizeof(PAL_HANDLE) * MAX_EPOLL_FDS);
  63. create_event(&epoll->event);
  64. INIT_LIST_HEAD(&epoll->fds);
  65. int vfd = set_new_fd_handle(hdl, flags, NULL);
  66. put_handle(hdl);
  67. return vfd;
  68. }
  69. /* the 'size' argument of epoll_create is not used */
  70. int shim_do_epoll_create (int size)
  71. {
  72. return shim_do_epoll_create1(0);
  73. }
  74. static void update_epoll (struct shim_epoll_handle * epoll)
  75. {
  76. struct shim_epoll_fd * tmp;
  77. int npals = 0;
  78. epoll->nread = 0;
  79. list_for_each_entry(tmp, &epoll->fds, list) {
  80. if (!tmp->pal_handle)
  81. continue;
  82. epoll->pal_fds[npals] = tmp->fd;
  83. epoll->pal_handles[npals] = tmp->pal_handle;
  84. npals++;
  85. if (tmp->handle->acc_mode & MAY_READ)
  86. epoll->nread++;
  87. }
  88. epoll->npals = npals;
  89. if (epoll->nwaiters)
  90. set_event(&epoll->event, epoll->nwaiters);
  91. }
  92. int shim_do_epoll_ctl (int epfd, int op, int fd,
  93. struct __kernel_epoll_event * event)
  94. {
  95. struct shim_thread * cur = get_cur_thread();
  96. int ret = 0;
  97. struct shim_handle * epoll_hdl = get_fd_handle(epfd, NULL, cur->handle_map);
  98. if (!epoll_hdl)
  99. return -EBADF;
  100. if (epoll_hdl->type != TYPE_EPOLL) {
  101. put_handle(epoll_hdl);
  102. return -EINVAL;
  103. }
  104. struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
  105. struct shim_epoll_fd * epoll_fd;
  106. lock(epoll_hdl->lock);
  107. switch (op) {
  108. case EPOLL_CTL_ADD: {
  109. list_for_each_entry(epoll_fd, &epoll->fds, list)
  110. if (epoll_fd->fd == fd) {
  111. ret = -EEXIST;
  112. goto out;
  113. }
  114. struct shim_handle * hdl = get_fd_handle(fd, NULL, cur->handle_map);
  115. if (!hdl) {
  116. ret = -EBADF;
  117. goto out;
  118. }
  119. if ((hdl->type != TYPE_PIPE && hdl->type != TYPE_SOCK) ||
  120. !hdl->pal_handle) {
  121. ret = -EPERM;
  122. put_handle(hdl);
  123. goto out;
  124. }
  125. if (epoll->nfds == MAX_EPOLL_FDS) {
  126. ret = -ENOSPC;
  127. put_handle(hdl);
  128. goto out;
  129. }
  130. epoll_fd = malloc(sizeof(struct shim_epoll_fd));
  131. epoll_fd->fd = fd;
  132. epoll_fd->events = event->events;
  133. epoll_fd->data = event->data;
  134. epoll_fd->revents = 0;
  135. epoll_fd->handle = hdl;
  136. epoll_fd->pal_handle = hdl->pal_handle;
  137. INIT_LIST_HEAD(&epoll_fd->list);
  138. list_add_tail(&epoll_fd->list, &epoll->fds);
  139. epoll->nfds++;
  140. goto update;
  141. }
  142. case EPOLL_CTL_MOD: {
  143. list_for_each_entry(epoll_fd, &epoll->fds, list)
  144. if (epoll_fd->fd == fd) {
  145. epoll_fd->events = event->events;
  146. epoll_fd->data = event->data;
  147. goto update;
  148. }
  149. ret = -ENOENT;
  150. goto out;
  151. }
  152. case EPOLL_CTL_DEL: {
  153. list_for_each_entry(epoll_fd, &epoll->fds, list)
  154. if (epoll_fd->fd == fd) {
  155. list_del(&epoll_fd->list);
  156. put_handle(epoll_fd->handle);
  157. free(epoll_fd);
  158. epoll->nfds--;
  159. goto update;
  160. }
  161. ret = -ENOENT;
  162. goto out;
  163. }
  164. default:
  165. ret = -ENOSYS;
  166. goto out;
  167. }
  168. update:
  169. update_epoll(epoll);
  170. out:
  171. unlock(epoll_hdl->lock);
  172. put_handle(epoll_hdl);
  173. return ret;
  174. }
  175. int shim_do_epoll_wait (int epfd, struct __kernel_epoll_event * events,
  176. int maxevents, int timeout)
  177. {
  178. int ret = 0;
  179. struct shim_handle * epoll_hdl = get_fd_handle(epfd, NULL, NULL);
  180. if (!epoll_hdl)
  181. return -EBADF;
  182. if (epoll_hdl->type != TYPE_EPOLL) {
  183. put_handle(epoll_hdl);
  184. return -EINVAL;
  185. }
  186. struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
  187. struct shim_epoll_fd * epoll_fd;
  188. int nevents = 0;
  189. int npals, nread;
  190. bool need_update = false;
  191. lock(epoll_hdl->lock);
  192. retry:
  193. if (!(npals = epoll->npals))
  194. goto reply;
  195. PAL_HANDLE * pal_handles = __alloca(sizeof(PAL_HANDLE) * (npals + 1));
  196. FDTYPE * fds = __alloca(sizeof(FDTYPE) * npals);
  197. memcpy(fds, epoll->pal_fds, sizeof(FDTYPE) * npals);
  198. memcpy(pal_handles, epoll->pal_handles, sizeof(PAL_HANDLE) * npals);
  199. pal_handles[npals] = epoll->event.event;
  200. if ((nread = epoll->nread))
  201. epoll->nwaiters++;
  202. unlock(epoll_hdl->lock);
  203. PAL_HANDLE polled = DkObjectsWaitAny(nread ? npals + 1 : npals, pal_handles,
  204. nread ? NO_TIMEOUT : 0);
  205. lock(epoll_hdl->lock);
  206. if (nread)
  207. epoll->nwaiters--;
  208. if (!polled)
  209. goto reply;
  210. if (polled == epoll->event.event) {
  211. wait_event(&epoll->event);
  212. goto retry;
  213. }
  214. PAL_STREAM_ATTR attr;
  215. if (!DkStreamAttributesQuerybyHandle(polled, &attr))
  216. goto reply;
  217. list_for_each_entry(epoll_fd, &epoll->fds, list)
  218. if (polled == epoll_fd->pal_handle) {
  219. debug("epoll: fd %d polled\n", epoll_fd->fd);
  220. if (attr.disconnected) {
  221. epoll_fd->revents |= EPOLLERR|EPOLLHUP|EPOLLRDHUP;
  222. epoll_fd->pal_handle = NULL;
  223. need_update = true;
  224. }
  225. if (attr.readable)
  226. epoll_fd->revents |= EPOLLIN;
  227. if (attr.writeable)
  228. epoll_fd->revents |= EPOLLOUT;
  229. break;
  230. }
  231. reply:
  232. list_for_each_entry(epoll_fd, &epoll->fds, list) {
  233. if (nevents == maxevents)
  234. break;
  235. if ((epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents) {
  236. events[nevents].events =
  237. (epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents;
  238. events[nevents].data = epoll_fd->data;
  239. nevents++;
  240. epoll_fd->revents &= ~epoll_fd->events;
  241. }
  242. }
  243. if (need_update)
  244. update_epoll(epoll);
  245. unlock(epoll_hdl->lock);
  246. ret = nevents;
  247. put_handle(epoll_hdl);
  248. return ret;
  249. }
  250. int shim_do_epoll_pwait (int epfd, struct __kernel_epoll_event * events,
  251. int maxevents, int timeout, const __sigset_t * sigmask,
  252. size_t sigsetsize)
  253. {
  254. int ret = shim_do_epoll_wait (epfd, events, maxevents, timeout);
  255. return ret;
  256. }
  257. static int epoll_close (struct shim_handle * hdl)
  258. {
  259. return 0;
  260. }
  261. struct shim_fs_ops epoll_fs_ops = {
  262. .close = &epoll_close,
  263. };
  264. struct shim_mount epoll_builtin_fs = { .type = "epoll",
  265. .fs_ops = &epoll_fs_ops, };