channel-watch.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * QEMU I/O channels watch helper APIs
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "io/channel-watch.h"
  22. typedef struct QIOChannelFDSource QIOChannelFDSource;
  23. struct QIOChannelFDSource {
  24. GSource parent;
  25. GPollFD fd;
  26. QIOChannel *ioc;
  27. GIOCondition condition;
  28. };
  29. #ifdef CONFIG_WIN32
  30. typedef struct QIOChannelSocketSource QIOChannelSocketSource;
  31. struct QIOChannelSocketSource {
  32. GSource parent;
  33. GPollFD fd;
  34. QIOChannel *ioc;
  35. SOCKET socket;
  36. int revents;
  37. GIOCondition condition;
  38. };
  39. #endif
  40. typedef struct QIOChannelFDPairSource QIOChannelFDPairSource;
  41. struct QIOChannelFDPairSource {
  42. GSource parent;
  43. GPollFD fdread;
  44. GPollFD fdwrite;
  45. QIOChannel *ioc;
  46. GIOCondition condition;
  47. };
  48. static gboolean
  49. qio_channel_fd_source_prepare(GSource *source G_GNUC_UNUSED,
  50. gint *timeout)
  51. {
  52. *timeout = -1;
  53. return FALSE;
  54. }
  55. static gboolean
  56. qio_channel_fd_source_check(GSource *source)
  57. {
  58. QIOChannelFDSource *ssource = (QIOChannelFDSource *)source;
  59. return ssource->fd.revents & ssource->condition;
  60. }
  61. static gboolean
  62. qio_channel_fd_source_dispatch(GSource *source,
  63. GSourceFunc callback,
  64. gpointer user_data)
  65. {
  66. QIOChannelFunc func = (QIOChannelFunc)callback;
  67. QIOChannelFDSource *ssource = (QIOChannelFDSource *)source;
  68. return (*func)(ssource->ioc,
  69. ssource->fd.revents & ssource->condition,
  70. user_data);
  71. }
  72. static void
  73. qio_channel_fd_source_finalize(GSource *source)
  74. {
  75. QIOChannelFDSource *ssource = (QIOChannelFDSource *)source;
  76. object_unref(OBJECT(ssource->ioc));
  77. }
  78. #ifdef CONFIG_WIN32
  79. static gboolean
  80. qio_channel_socket_source_prepare(GSource *source G_GNUC_UNUSED,
  81. gint *timeout)
  82. {
  83. *timeout = -1;
  84. return FALSE;
  85. }
  86. /*
  87. * NB, this impl only works when the socket is in non-blocking
  88. * mode on Win32
  89. */
  90. static gboolean
  91. qio_channel_socket_source_check(GSource *source)
  92. {
  93. static struct timeval tv0;
  94. QIOChannelSocketSource *ssource = (QIOChannelSocketSource *)source;
  95. WSANETWORKEVENTS ev;
  96. fd_set rfds, wfds, xfds;
  97. if (!ssource->condition) {
  98. return 0;
  99. }
  100. WSAEnumNetworkEvents(ssource->socket, ssource->ioc->event, &ev);
  101. FD_ZERO(&rfds);
  102. FD_ZERO(&wfds);
  103. FD_ZERO(&xfds);
  104. if (ssource->condition & G_IO_IN) {
  105. FD_SET((SOCKET)ssource->socket, &rfds);
  106. }
  107. if (ssource->condition & G_IO_OUT) {
  108. FD_SET((SOCKET)ssource->socket, &wfds);
  109. }
  110. if (ssource->condition & G_IO_PRI) {
  111. FD_SET((SOCKET)ssource->socket, &xfds);
  112. }
  113. ssource->revents = 0;
  114. if (select(0, &rfds, &wfds, &xfds, &tv0) == 0) {
  115. return 0;
  116. }
  117. if (FD_ISSET(ssource->socket, &rfds)) {
  118. ssource->revents |= G_IO_IN;
  119. }
  120. if (FD_ISSET(ssource->socket, &wfds)) {
  121. ssource->revents |= G_IO_OUT;
  122. }
  123. if (FD_ISSET(ssource->socket, &xfds)) {
  124. ssource->revents |= G_IO_PRI;
  125. }
  126. return ssource->revents;
  127. }
  128. static gboolean
  129. qio_channel_socket_source_dispatch(GSource *source,
  130. GSourceFunc callback,
  131. gpointer user_data)
  132. {
  133. QIOChannelFunc func = (QIOChannelFunc)callback;
  134. QIOChannelSocketSource *ssource = (QIOChannelSocketSource *)source;
  135. return (*func)(ssource->ioc, ssource->revents, user_data);
  136. }
  137. static void
  138. qio_channel_socket_source_finalize(GSource *source)
  139. {
  140. QIOChannelSocketSource *ssource = (QIOChannelSocketSource *)source;
  141. object_unref(OBJECT(ssource->ioc));
  142. }
  143. GSourceFuncs qio_channel_socket_source_funcs = {
  144. qio_channel_socket_source_prepare,
  145. qio_channel_socket_source_check,
  146. qio_channel_socket_source_dispatch,
  147. qio_channel_socket_source_finalize
  148. };
  149. #endif
  150. static gboolean
  151. qio_channel_fd_pair_source_prepare(GSource *source G_GNUC_UNUSED,
  152. gint *timeout)
  153. {
  154. *timeout = -1;
  155. return FALSE;
  156. }
  157. static gboolean
  158. qio_channel_fd_pair_source_check(GSource *source)
  159. {
  160. QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
  161. GIOCondition poll_condition = ssource->fdread.revents |
  162. ssource->fdwrite.revents;
  163. return poll_condition & ssource->condition;
  164. }
  165. static gboolean
  166. qio_channel_fd_pair_source_dispatch(GSource *source,
  167. GSourceFunc callback,
  168. gpointer user_data)
  169. {
  170. QIOChannelFunc func = (QIOChannelFunc)callback;
  171. QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
  172. GIOCondition poll_condition = ssource->fdread.revents |
  173. ssource->fdwrite.revents;
  174. return (*func)(ssource->ioc,
  175. poll_condition & ssource->condition,
  176. user_data);
  177. }
  178. static void
  179. qio_channel_fd_pair_source_finalize(GSource *source)
  180. {
  181. QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
  182. object_unref(OBJECT(ssource->ioc));
  183. }
  184. GSourceFuncs qio_channel_fd_source_funcs = {
  185. qio_channel_fd_source_prepare,
  186. qio_channel_fd_source_check,
  187. qio_channel_fd_source_dispatch,
  188. qio_channel_fd_source_finalize
  189. };
  190. GSourceFuncs qio_channel_fd_pair_source_funcs = {
  191. qio_channel_fd_pair_source_prepare,
  192. qio_channel_fd_pair_source_check,
  193. qio_channel_fd_pair_source_dispatch,
  194. qio_channel_fd_pair_source_finalize
  195. };
  196. GSource *qio_channel_create_fd_watch(QIOChannel *ioc,
  197. int fd,
  198. GIOCondition condition)
  199. {
  200. GSource *source;
  201. QIOChannelFDSource *ssource;
  202. source = g_source_new(&qio_channel_fd_source_funcs,
  203. sizeof(QIOChannelFDSource));
  204. ssource = (QIOChannelFDSource *)source;
  205. ssource->ioc = ioc;
  206. object_ref(OBJECT(ioc));
  207. ssource->condition = condition;
  208. #ifdef CONFIG_WIN32
  209. ssource->fd.fd = (gint64)_get_osfhandle(fd);
  210. #else
  211. ssource->fd.fd = fd;
  212. #endif
  213. ssource->fd.events = condition;
  214. g_source_add_poll(source, &ssource->fd);
  215. return source;
  216. }
  217. #ifdef CONFIG_WIN32
  218. GSource *qio_channel_create_socket_watch(QIOChannel *ioc,
  219. int socket,
  220. GIOCondition condition)
  221. {
  222. GSource *source;
  223. QIOChannelSocketSource *ssource;
  224. #ifdef WIN32
  225. WSAEventSelect(socket, ioc->event,
  226. FD_READ | FD_ACCEPT | FD_CLOSE |
  227. FD_CONNECT | FD_WRITE | FD_OOB);
  228. #endif
  229. source = g_source_new(&qio_channel_socket_source_funcs,
  230. sizeof(QIOChannelSocketSource));
  231. ssource = (QIOChannelSocketSource *)source;
  232. ssource->ioc = ioc;
  233. object_ref(OBJECT(ioc));
  234. ssource->condition = condition;
  235. ssource->socket = socket;
  236. ssource->revents = 0;
  237. ssource->fd.fd = (gintptr)ioc->event;
  238. ssource->fd.events = G_IO_IN;
  239. g_source_add_poll(source, &ssource->fd);
  240. return source;
  241. }
  242. #else
  243. GSource *qio_channel_create_socket_watch(QIOChannel *ioc,
  244. int socket,
  245. GIOCondition condition)
  246. {
  247. return qio_channel_create_fd_watch(ioc, socket, condition);
  248. }
  249. #endif
  250. GSource *qio_channel_create_fd_pair_watch(QIOChannel *ioc,
  251. int fdread,
  252. int fdwrite,
  253. GIOCondition condition)
  254. {
  255. GSource *source;
  256. QIOChannelFDPairSource *ssource;
  257. source = g_source_new(&qio_channel_fd_pair_source_funcs,
  258. sizeof(QIOChannelFDPairSource));
  259. ssource = (QIOChannelFDPairSource *)source;
  260. ssource->ioc = ioc;
  261. object_ref(OBJECT(ioc));
  262. ssource->condition = condition;
  263. #ifdef CONFIG_WIN32
  264. ssource->fdread.fd = (gint64)_get_osfhandle(fdread);
  265. ssource->fdwrite.fd = (gint64)_get_osfhandle(fdwrite);
  266. #else
  267. ssource->fdread.fd = fdread;
  268. ssource->fdwrite.fd = fdwrite;
  269. #endif
  270. ssource->fdread.events = condition & G_IO_IN;
  271. ssource->fdwrite.events = condition & G_IO_OUT;
  272. g_source_add_poll(source, &ssource->fdread);
  273. g_source_add_poll(source, &ssource->fdwrite);
  274. return source;
  275. }