shim_epoll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. put_handle(epoll_hdl);
  209. debug("delete handle %p from epoll handle %p\n",
  210. hdl, epoll);
  211. LISTP_DEL(epoll_fd, &epoll->fds, list);
  212. epoll->nfds--;
  213. free(epoll_fd);
  214. goto update;
  215. }
  216. ret = -ENOENT;
  217. goto out;
  218. }
  219. default:
  220. ret = -ENOSYS;
  221. goto out;
  222. }
  223. update:
  224. update_epoll(epoll);
  225. out:
  226. unlock(&epoll_hdl->lock);
  227. put_handle(epoll_hdl);
  228. return ret;
  229. }
  230. int shim_do_epoll_wait (int epfd, struct __kernel_epoll_event * events,
  231. int maxevents, int timeout)
  232. {
  233. int ret = 0;
  234. struct shim_handle * epoll_hdl = get_fd_handle(epfd, NULL, NULL);
  235. if (!epoll_hdl)
  236. return -EBADF;
  237. if (epoll_hdl->type != TYPE_EPOLL) {
  238. put_handle(epoll_hdl);
  239. return -EINVAL;
  240. }
  241. struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
  242. struct shim_epoll_fd * epoll_fd;
  243. int nevents = 0;
  244. int npals, nread;
  245. bool need_update = false;
  246. lock(&epoll_hdl->lock);
  247. retry:
  248. if (!(npals = epoll->npals))
  249. goto reply;
  250. PAL_HANDLE * pal_handles = __alloca(sizeof(PAL_HANDLE) * (npals + 1));
  251. FDTYPE * fds = __alloca(sizeof(FDTYPE) * npals);
  252. memcpy(fds, epoll->pal_fds, sizeof(FDTYPE) * npals);
  253. memcpy(pal_handles, epoll->pal_handles, sizeof(PAL_HANDLE) * npals);
  254. pal_handles[npals] = epoll->event.event;
  255. if ((nread = epoll->nread))
  256. epoll->nwaiters++;
  257. unlock(&epoll_hdl->lock);
  258. PAL_HANDLE polled = DkObjectsWaitAny(nread ? npals + 1 : npals, pal_handles,
  259. nread ? (timeout == -1 ? NO_TIMEOUT : (PAL_NUM) timeout) : 0);
  260. lock(&epoll_hdl->lock);
  261. if (nread)
  262. epoll->nwaiters--;
  263. if (!polled)
  264. goto reply;
  265. if (polled == epoll->event.event) {
  266. wait_event(&epoll->event);
  267. goto retry;
  268. }
  269. PAL_STREAM_ATTR attr;
  270. if (!DkStreamAttributesQueryByHandle(polled, &attr))
  271. goto reply;
  272. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
  273. if (polled == epoll_fd->pal_handle) {
  274. debug("epoll: fd %d (handle %p) polled\n", epoll_fd->fd,
  275. epoll_fd->handle);
  276. if (attr.disconnected) {
  277. epoll_fd->revents |= EPOLLERR|EPOLLHUP|EPOLLRDHUP;
  278. epoll_fd->pal_handle = NULL;
  279. need_update = true;
  280. }
  281. if (attr.readable)
  282. epoll_fd->revents |= EPOLLIN;
  283. if (attr.writable)
  284. epoll_fd->revents |= EPOLLOUT;
  285. break;
  286. }
  287. reply:
  288. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
  289. if (nevents == maxevents)
  290. break;
  291. if ((epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents) {
  292. events[nevents].events =
  293. (epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents;
  294. events[nevents].data = epoll_fd->data;
  295. nevents++;
  296. epoll_fd->revents &= ~epoll_fd->events;
  297. }
  298. }
  299. if (need_update)
  300. update_epoll(epoll);
  301. unlock(&epoll_hdl->lock);
  302. ret = nevents;
  303. put_handle(epoll_hdl);
  304. return ret;
  305. }
  306. int shim_do_epoll_pwait (int epfd, struct __kernel_epoll_event * events,
  307. int maxevents, int timeout, const __sigset_t * sigmask,
  308. size_t sigsetsize)
  309. {
  310. int ret = shim_do_epoll_wait (epfd, events, maxevents, timeout);
  311. return ret;
  312. }
  313. static int epoll_close (struct shim_handle * hdl)
  314. {
  315. return 0;
  316. }
  317. struct shim_fs_ops epoll_fs_ops = {
  318. .close = &epoll_close,
  319. };
  320. struct shim_mount epoll_builtin_fs = { .type = "epoll",
  321. .fs_ops = &epoll_fs_ops, };
  322. BEGIN_CP_FUNC(epoll_fd)
  323. {
  324. assert(size == sizeof(LISTP_TYPE(shim_epoll_fd)));
  325. LISTP_TYPE(shim_epoll_fd) * old_list = (LISTP_TYPE(shim_epoll_fd) *) obj;
  326. LISTP_TYPE(shim_epoll_fd) * new_list = (LISTP_TYPE(shim_epoll_fd) *) objp;
  327. struct shim_epoll_fd * epoll_fd;
  328. debug("checkpoint epoll: %p -> %p (base = 0x%08lx)\n", old_list, new_list, base);
  329. INIT_LISTP(new_list);
  330. LISTP_FOR_EACH_ENTRY(epoll_fd, old_list, list) {
  331. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_epoll_fd));
  332. struct shim_epoll_fd * new_epoll_fd =
  333. (struct shim_epoll_fd *) (base + off);
  334. new_epoll_fd->fd = epoll_fd->fd;
  335. new_epoll_fd->events = epoll_fd->events;
  336. new_epoll_fd->data = epoll_fd->data;
  337. new_epoll_fd->revents = epoll_fd->revents;
  338. new_epoll_fd->pal_handle = NULL;
  339. LISTP_ADD(new_epoll_fd, new_list, list);
  340. DO_CP(handle, epoll_fd->handle, &new_epoll_fd->handle);
  341. }
  342. ADD_CP_FUNC_ENTRY((ptr_t) objp - base);
  343. }
  344. END_CP_FUNC(epoll_fd)
  345. BEGIN_RS_FUNC(epoll_fd)
  346. {
  347. LISTP_TYPE(shim_epoll_fd) * list = (void *) (base + GET_CP_FUNC_ENTRY());
  348. struct shim_epoll_fd * epoll_fd;
  349. CP_REBASE(*list);
  350. LISTP_FOR_EACH_ENTRY(epoll_fd, list, list) {
  351. CP_REBASE(epoll_fd->handle);
  352. CP_REBASE(epoll_fd->back);
  353. epoll_fd->pal_handle = epoll_fd->handle->pal_handle;
  354. CP_REBASE(epoll_fd->list);
  355. DEBUG_RS("fd=%d,path=%s,type=%s,uri=%s",
  356. epoll_fd->fd, qstrgetstr(&epoll_fd->handle->path),
  357. epoll_fd->handle->fs_type,
  358. qstrgetstr(&epoll_fd->handle->uri));
  359. }
  360. }
  361. END_RS_FUNC(epoll_fd)