shim_epoll.c 13 KB

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