shim_epoll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_epoll.c
  15. *
  16. * Implementation of system call "epoll_create", "epoll_create1", "epoll_ctl"
  17. * and "epoll_wait".
  18. */
  19. #include <shim_internal.h>
  20. #include <shim_table.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_fs.h>
  24. #include <shim_checkpoint.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. #include <linux/eventpoll.h>
  29. /* Avoid duplicated definitions */
  30. #ifndef EPOLLIN
  31. #define EPOLLIN 0x001
  32. #define EPOLLPRI 0x002
  33. #define EPOLLOUT 0x004
  34. #define EPOLLRDNORM 0x040
  35. #define EPOLLRDBAND 0x080
  36. #define EPOLLWRNORM 0x100
  37. #define EPOLLERBAND 0x200
  38. #define EPOLLERR 0x008
  39. #define EPOLLHUP 0x010
  40. #define EPOLLRDHUP 0x2000
  41. #endif
  42. #define MAX_EPOLL_FDS 1024
  43. struct shim_mount epoll_builtin_fs;
  44. /* shim_epoll_fds are linked as a list (by the list field),
  45. * hanging off of a shim_epoll_handle (by the fds field) */
  46. struct shim_epoll_fd {
  47. FDTYPE fd;
  48. unsigned int events;
  49. __u64 data;
  50. unsigned int revents;
  51. struct shim_handle * handle;
  52. struct shim_handle * epoll;
  53. PAL_HANDLE pal_handle;
  54. LIST_TYPE(shim_epoll_fd) list;
  55. LIST_TYPE(shim_epoll_fd) back;
  56. };
  57. int shim_do_epoll_create1 (int flags)
  58. {
  59. if ((flags & ~EPOLL_CLOEXEC))
  60. return -EINVAL;
  61. struct shim_handle * hdl = get_new_handle();
  62. if (!hdl)
  63. return -ENOMEM;
  64. struct shim_epoll_handle * epoll = &hdl->info.epoll;
  65. hdl->type = TYPE_EPOLL;
  66. set_handle_fs(hdl, &epoll_builtin_fs);
  67. epoll->maxfds = MAX_EPOLL_FDS;
  68. epoll->nfds = 0;
  69. epoll->pal_fds = malloc(sizeof(FDTYPE) * MAX_EPOLL_FDS);
  70. epoll->pal_handles = malloc(sizeof(PAL_HANDLE) * MAX_EPOLL_FDS);
  71. create_event(&epoll->event);
  72. INIT_LISTP(&epoll->fds);
  73. int vfd = set_new_fd_handle(hdl, (flags & EPOLL_CLOEXEC) ? FD_CLOEXEC : 0,
  74. NULL);
  75. put_handle(hdl);
  76. return vfd;
  77. }
  78. /* the 'size' argument of epoll_create is not used */
  79. int shim_do_epoll_create (int size)
  80. {
  81. if (size < 0)
  82. return -EINVAL;
  83. return shim_do_epoll_create1(0);
  84. }
  85. static void update_epoll (struct shim_epoll_handle * epoll)
  86. {
  87. struct shim_epoll_fd * tmp;
  88. int npals = 0;
  89. epoll->nread = 0;
  90. LISTP_FOR_EACH_ENTRY(tmp, &epoll->fds, list) {
  91. if (!tmp->pal_handle)
  92. continue;
  93. debug("found handle %p (pal handle %p) from epoll handle %p\n",
  94. tmp->handle, tmp->pal_handle, epoll);
  95. epoll->pal_fds[npals] = tmp->fd;
  96. epoll->pal_handles[npals] = tmp->pal_handle;
  97. npals++;
  98. if (tmp->handle->acc_mode & MAY_READ)
  99. epoll->nread++;
  100. }
  101. epoll->npals = npals;
  102. if (epoll->nwaiters)
  103. set_event(&epoll->event, epoll->nwaiters);
  104. }
  105. int delete_from_epoll_handles (struct shim_handle * handle)
  106. {
  107. while (1) {
  108. lock(&handle->lock);
  109. if (LISTP_EMPTY(&handle->epolls)) {
  110. unlock(&handle->lock);
  111. break;
  112. }
  113. struct shim_epoll_fd * epoll_fd = LISTP_FIRST_ENTRY(&handle->epolls,
  114. struct shim_epoll_fd, back);
  115. LISTP_DEL(epoll_fd, &handle->epolls, back);
  116. unlock(&handle->lock);
  117. put_handle(handle);
  118. struct shim_handle * epoll_hdl = epoll_fd->epoll;
  119. struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
  120. debug("delete handle %p from epoll handle %p\n", handle,
  121. &epoll_hdl->info.epoll);
  122. lock(&epoll_hdl->lock);
  123. LISTP_DEL(epoll_fd, &epoll->fds, list);
  124. free(epoll_fd);
  125. epoll_hdl->info.epoll.nfds--;
  126. update_epoll(&epoll_hdl->info.epoll);
  127. unlock(&epoll_hdl->lock);
  128. put_handle(epoll_hdl);
  129. }
  130. return 0;
  131. }
  132. int shim_do_epoll_ctl (int epfd, int op, int fd,
  133. struct __kernel_epoll_event * event)
  134. {
  135. struct shim_thread * cur = get_cur_thread();
  136. int ret = 0;
  137. struct shim_handle * epoll_hdl = get_fd_handle(epfd, NULL, cur->handle_map);
  138. if (!epoll_hdl)
  139. return -EBADF;
  140. if (epoll_hdl->type != TYPE_EPOLL) {
  141. put_handle(epoll_hdl);
  142. return -EINVAL;
  143. }
  144. struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
  145. struct shim_epoll_fd * epoll_fd;
  146. lock(&epoll_hdl->lock);
  147. switch (op) {
  148. case EPOLL_CTL_ADD: {
  149. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
  150. if (epoll_fd->fd == fd) {
  151. ret = -EEXIST;
  152. goto out;
  153. }
  154. struct shim_handle * hdl = get_fd_handle(fd, NULL, cur->handle_map);
  155. if (!hdl) {
  156. ret = -EBADF;
  157. goto out;
  158. }
  159. if ((hdl->type != TYPE_PIPE && hdl->type != TYPE_SOCK) ||
  160. !hdl->pal_handle) {
  161. ret = -EPERM;
  162. put_handle(hdl);
  163. goto out;
  164. }
  165. if (epoll->nfds == MAX_EPOLL_FDS) {
  166. ret = -ENOSPC;
  167. put_handle(hdl);
  168. goto out;
  169. }
  170. debug("add handle %p to epoll handle %p\n", hdl, epoll);
  171. epoll_fd = malloc(sizeof(struct shim_epoll_fd));
  172. epoll_fd->fd = fd;
  173. epoll_fd->events = event->events;
  174. epoll_fd->data = event->data;
  175. epoll_fd->revents = 0;
  176. epoll_fd->handle = hdl;
  177. epoll_fd->epoll = epoll_hdl;
  178. epoll_fd->pal_handle = hdl->pal_handle;
  179. /* Register the epoll handle */
  180. get_handle(epoll_hdl);
  181. lock(&hdl->lock);
  182. INIT_LIST_HEAD(epoll_fd, back);
  183. LISTP_ADD_TAIL(epoll_fd, &hdl->epolls, back);
  184. unlock(&hdl->lock);
  185. INIT_LIST_HEAD(epoll_fd, list);
  186. LISTP_ADD_TAIL(epoll_fd, &epoll->fds, list);
  187. epoll->nfds++;
  188. goto update;
  189. }
  190. case EPOLL_CTL_MOD: {
  191. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
  192. if (epoll_fd->fd == fd) {
  193. epoll_fd->events = event->events;
  194. epoll_fd->data = event->data;
  195. goto update;
  196. }
  197. ret = -ENOENT;
  198. goto out;
  199. }
  200. case EPOLL_CTL_DEL: {
  201. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
  202. if (epoll_fd->fd == fd) {
  203. struct shim_handle * hdl = epoll_fd->handle;
  204. /* Unregister the epoll handle */
  205. lock(&hdl->lock);
  206. LISTP_DEL(epoll_fd, &hdl->epolls, back);
  207. unlock(&hdl->lock);
  208. debug("delete handle %p from epoll handle %p\n",
  209. hdl, epoll);
  210. put_handle(epoll_hdl);
  211. put_handle(hdl);
  212. LISTP_DEL(epoll_fd, &epoll->fds, list);
  213. epoll->nfds--;
  214. free(epoll_fd);
  215. goto update;
  216. }
  217. ret = -ENOENT;
  218. goto out;
  219. }
  220. default:
  221. ret = -ENOSYS;
  222. goto out;
  223. }
  224. update:
  225. update_epoll(epoll);
  226. out:
  227. unlock(&epoll_hdl->lock);
  228. put_handle(epoll_hdl);
  229. return ret;
  230. }
  231. int shim_do_epoll_wait (int epfd, struct __kernel_epoll_event * events,
  232. int maxevents, int timeout_ms)
  233. {
  234. int ret = 0;
  235. struct shim_handle * epoll_hdl = get_fd_handle(epfd, NULL, NULL);
  236. if (!epoll_hdl)
  237. return -EBADF;
  238. if (epoll_hdl->type != TYPE_EPOLL) {
  239. put_handle(epoll_hdl);
  240. return -EINVAL;
  241. }
  242. struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
  243. struct shim_epoll_fd * epoll_fd;
  244. int nevents = 0;
  245. int npals, nread;
  246. bool need_update = false;
  247. lock(&epoll_hdl->lock);
  248. retry:
  249. if (!(npals = epoll->npals))
  250. goto reply;
  251. PAL_HANDLE * pal_handles = __alloca(sizeof(PAL_HANDLE) * (npals + 1));
  252. FDTYPE * fds = __alloca(sizeof(FDTYPE) * npals);
  253. memcpy(fds, epoll->pal_fds, sizeof(FDTYPE) * npals);
  254. memcpy(pal_handles, epoll->pal_handles, sizeof(PAL_HANDLE) * npals);
  255. pal_handles[npals] = epoll->event.event;
  256. if ((nread = epoll->nread))
  257. epoll->nwaiters++;
  258. unlock(&epoll_hdl->lock);
  259. PAL_NUM pal_timeout = timeout_ms == -1 ? NO_TIMEOUT : (PAL_NUM) timeout_ms * 1000;
  260. PAL_HANDLE polled = DkObjectsWaitAny(nread ? npals + 1 : npals, pal_handles,
  261. nread ? pal_timeout : 0);
  262. lock(&epoll_hdl->lock);
  263. if (nread)
  264. epoll->nwaiters--;
  265. if (!polled)
  266. goto reply;
  267. if (polled == epoll->event.event) {
  268. wait_event(&epoll->event);
  269. goto retry;
  270. }
  271. PAL_STREAM_ATTR attr;
  272. if (!DkStreamAttributesQueryByHandle(polled, &attr))
  273. goto reply;
  274. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
  275. if (polled == epoll_fd->pal_handle) {
  276. debug("epoll: fd %d (handle %p) polled\n", epoll_fd->fd,
  277. epoll_fd->handle);
  278. if (attr.disconnected) {
  279. epoll_fd->revents |= EPOLLERR|EPOLLHUP|EPOLLRDHUP;
  280. epoll_fd->pal_handle = NULL;
  281. need_update = true;
  282. }
  283. if (attr.readable)
  284. epoll_fd->revents |= EPOLLIN;
  285. if (attr.writable)
  286. epoll_fd->revents |= EPOLLOUT;
  287. break;
  288. }
  289. reply:
  290. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
  291. if (nevents == maxevents)
  292. break;
  293. if ((epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents) {
  294. events[nevents].events =
  295. (epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents;
  296. events[nevents].data = epoll_fd->data;
  297. nevents++;
  298. epoll_fd->revents &= ~epoll_fd->events;
  299. }
  300. }
  301. if (need_update)
  302. update_epoll(epoll);
  303. unlock(&epoll_hdl->lock);
  304. ret = nevents;
  305. put_handle(epoll_hdl);
  306. return ret;
  307. }
  308. int shim_do_epoll_pwait (int epfd, struct __kernel_epoll_event * events,
  309. int maxevents, int timeout_ms, const __sigset_t * sigmask,
  310. size_t sigsetsize)
  311. {
  312. __UNUSED(sigmask);
  313. __UNUSED(sigsetsize);
  314. int ret = shim_do_epoll_wait (epfd, events, maxevents, timeout_ms);
  315. return ret;
  316. }
  317. static int epoll_close (struct shim_handle * hdl)
  318. {
  319. __UNUSED(hdl);
  320. return 0;
  321. }
  322. struct shim_fs_ops epoll_fs_ops = {
  323. .close = &epoll_close,
  324. };
  325. struct shim_mount epoll_builtin_fs = { .type = "epoll",
  326. .fs_ops = &epoll_fs_ops, };
  327. BEGIN_CP_FUNC(epoll_fd)
  328. {
  329. assert(size == sizeof(LISTP_TYPE(shim_epoll_fd)));
  330. LISTP_TYPE(shim_epoll_fd) * old_list = (LISTP_TYPE(shim_epoll_fd) *) obj;
  331. LISTP_TYPE(shim_epoll_fd) * new_list = (LISTP_TYPE(shim_epoll_fd) *) objp;
  332. struct shim_epoll_fd * epoll_fd;
  333. debug("checkpoint epoll: %p -> %p (base = 0x%08lx)\n", old_list, new_list, base);
  334. INIT_LISTP(new_list);
  335. LISTP_FOR_EACH_ENTRY(epoll_fd, old_list, list) {
  336. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_epoll_fd));
  337. struct shim_epoll_fd * new_epoll_fd =
  338. (struct shim_epoll_fd *) (base + off);
  339. new_epoll_fd->fd = epoll_fd->fd;
  340. new_epoll_fd->events = epoll_fd->events;
  341. new_epoll_fd->data = epoll_fd->data;
  342. new_epoll_fd->revents = epoll_fd->revents;
  343. new_epoll_fd->pal_handle = NULL;
  344. LISTP_ADD(new_epoll_fd, new_list, list);
  345. DO_CP(handle, epoll_fd->handle, &new_epoll_fd->handle);
  346. }
  347. ADD_CP_FUNC_ENTRY((ptr_t) objp - base);
  348. }
  349. END_CP_FUNC(epoll_fd)
  350. BEGIN_RS_FUNC(epoll_fd)
  351. {
  352. __UNUSED(offset);
  353. LISTP_TYPE(shim_epoll_fd) * list = (void *) (base + GET_CP_FUNC_ENTRY());
  354. struct shim_epoll_fd * epoll_fd;
  355. CP_REBASE(*list);
  356. LISTP_FOR_EACH_ENTRY(epoll_fd, list, list) {
  357. CP_REBASE(epoll_fd->handle);
  358. CP_REBASE(epoll_fd->back);
  359. epoll_fd->pal_handle = epoll_fd->handle->pal_handle;
  360. CP_REBASE(epoll_fd->list);
  361. DEBUG_RS("fd=%d,path=%s,type=%s,uri=%s",
  362. epoll_fd->fd, qstrgetstr(&epoll_fd->handle->path),
  363. epoll_fd->handle->fs_type,
  364. qstrgetstr(&epoll_fd->handle->uri));
  365. }
  366. }
  367. END_RS_FUNC(epoll_fd)