channel-watch.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.1 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. fd_set rfds, wfds, xfds;
  96. if (!ssource->condition) {
  97. return 0;
  98. }
  99. FD_ZERO(&rfds);
  100. FD_ZERO(&wfds);
  101. FD_ZERO(&xfds);
  102. if (ssource->condition & G_IO_IN) {
  103. FD_SET(ssource->socket, &rfds);
  104. }
  105. if (ssource->condition & G_IO_OUT) {
  106. FD_SET(ssource->socket, &wfds);
  107. }
  108. if (ssource->condition & G_IO_PRI) {
  109. FD_SET(ssource->socket, &xfds);
  110. }
  111. ssource->revents = 0;
  112. if (select(0, &rfds, &wfds, &xfds, &tv0) == 0) {
  113. return 0;
  114. }
  115. if (FD_ISSET(ssource->socket, &rfds)) {
  116. ssource->revents |= G_IO_IN;
  117. }
  118. if (FD_ISSET(ssource->socket, &wfds)) {
  119. ssource->revents |= G_IO_OUT;
  120. }
  121. if (FD_ISSET(ssource->socket, &xfds)) {
  122. ssource->revents |= G_IO_PRI;
  123. }
  124. return ssource->revents;
  125. }
  126. static gboolean
  127. qio_channel_socket_source_dispatch(GSource *source,
  128. GSourceFunc callback,
  129. gpointer user_data)
  130. {
  131. QIOChannelFunc func = (QIOChannelFunc)callback;
  132. QIOChannelSocketSource *ssource = (QIOChannelSocketSource *)source;
  133. return (*func)(ssource->ioc, ssource->revents, user_data);
  134. }
  135. static void
  136. qio_channel_socket_source_finalize(GSource *source)
  137. {
  138. QIOChannelSocketSource *ssource = (QIOChannelSocketSource *)source;
  139. object_unref(OBJECT(ssource->ioc));
  140. }
  141. GSourceFuncs qio_channel_socket_source_funcs = {
  142. qio_channel_socket_source_prepare,
  143. qio_channel_socket_source_check,
  144. qio_channel_socket_source_dispatch,
  145. qio_channel_socket_source_finalize
  146. };
  147. #endif
  148. static gboolean
  149. qio_channel_fd_pair_source_prepare(GSource *source G_GNUC_UNUSED,
  150. gint *timeout)
  151. {
  152. *timeout = -1;
  153. return FALSE;
  154. }
  155. static gboolean
  156. qio_channel_fd_pair_source_check(GSource *source)
  157. {
  158. QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
  159. GIOCondition poll_condition = ssource->fdread.revents |
  160. ssource->fdwrite.revents;
  161. return poll_condition & ssource->condition;
  162. }
  163. static gboolean
  164. qio_channel_fd_pair_source_dispatch(GSource *source,
  165. GSourceFunc callback,
  166. gpointer user_data)
  167. {
  168. QIOChannelFunc func = (QIOChannelFunc)callback;
  169. QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
  170. GIOCondition poll_condition = ssource->fdread.revents |
  171. ssource->fdwrite.revents;
  172. return (*func)(ssource->ioc,
  173. poll_condition & ssource->condition,
  174. user_data);
  175. }
  176. static void
  177. qio_channel_fd_pair_source_finalize(GSource *source)
  178. {
  179. QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
  180. object_unref(OBJECT(ssource->ioc));
  181. }
  182. GSourceFuncs qio_channel_fd_source_funcs = {
  183. qio_channel_fd_source_prepare,
  184. qio_channel_fd_source_check,
  185. qio_channel_fd_source_dispatch,
  186. qio_channel_fd_source_finalize
  187. };
  188. GSourceFuncs qio_channel_fd_pair_source_funcs = {
  189. qio_channel_fd_pair_source_prepare,
  190. qio_channel_fd_pair_source_check,
  191. qio_channel_fd_pair_source_dispatch,
  192. qio_channel_fd_pair_source_finalize
  193. };
  194. GSource *qio_channel_create_fd_watch(QIOChannel *ioc,
  195. int fd,
  196. GIOCondition condition)
  197. {
  198. GSource *source;
  199. QIOChannelFDSource *ssource;
  200. source = g_source_new(&qio_channel_fd_source_funcs,
  201. sizeof(QIOChannelFDSource));
  202. ssource = (QIOChannelFDSource *)source;
  203. ssource->ioc = ioc;
  204. object_ref(OBJECT(ioc));
  205. ssource->condition = condition;
  206. #ifdef CONFIG_WIN32
  207. ssource->fd.fd = (gint64)_get_osfhandle(fd);
  208. #else
  209. ssource->fd.fd = fd;
  210. #endif
  211. ssource->fd.events = condition;
  212. g_source_add_poll(source, &ssource->fd);
  213. return source;
  214. }
  215. #ifdef CONFIG_WIN32
  216. GSource *qio_channel_create_socket_watch(QIOChannel *ioc,
  217. int sockfd,
  218. GIOCondition condition)
  219. {
  220. GSource *source;
  221. QIOChannelSocketSource *ssource;
  222. qemu_socket_select(sockfd, ioc->event,
  223. FD_READ | FD_ACCEPT | FD_CLOSE |
  224. FD_CONNECT | FD_WRITE | FD_OOB, NULL);
  225. source = g_source_new(&qio_channel_socket_source_funcs,
  226. sizeof(QIOChannelSocketSource));
  227. ssource = (QIOChannelSocketSource *)source;
  228. ssource->ioc = ioc;
  229. object_ref(OBJECT(ioc));
  230. ssource->condition = condition;
  231. ssource->socket = _get_osfhandle(sockfd);
  232. ssource->revents = 0;
  233. ssource->fd.fd = (gintptr)ioc->event;
  234. ssource->fd.events = G_IO_IN;
  235. g_source_add_poll(source, &ssource->fd);
  236. return source;
  237. }
  238. #else
  239. GSource *qio_channel_create_socket_watch(QIOChannel *ioc,
  240. int socket,
  241. GIOCondition condition)
  242. {
  243. return qio_channel_create_fd_watch(ioc, socket, condition);
  244. }
  245. #endif
  246. GSource *qio_channel_create_fd_pair_watch(QIOChannel *ioc,
  247. int fdread,
  248. int fdwrite,
  249. GIOCondition condition)
  250. {
  251. GSource *source;
  252. QIOChannelFDPairSource *ssource;
  253. source = g_source_new(&qio_channel_fd_pair_source_funcs,
  254. sizeof(QIOChannelFDPairSource));
  255. ssource = (QIOChannelFDPairSource *)source;
  256. ssource->ioc = ioc;
  257. object_ref(OBJECT(ioc));
  258. ssource->condition = condition;
  259. #ifdef CONFIG_WIN32
  260. ssource->fdread.fd = (gint64)_get_osfhandle(fdread);
  261. ssource->fdwrite.fd = (gint64)_get_osfhandle(fdwrite);
  262. #else
  263. ssource->fdread.fd = fdread;
  264. ssource->fdwrite.fd = fdwrite;
  265. #endif
  266. ssource->fdread.events = condition & G_IO_IN;
  267. ssource->fdwrite.events = condition & G_IO_OUT;
  268. g_source_add_poll(source, &ssource->fdread);
  269. g_source_add_poll(source, &ssource->fdwrite);
  270. return source;
  271. }