shim_epoll.c 11 KB

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