shim_epoll.c 13 KB

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