2
0

channel-null.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * QEMU I/O channels null driver
  3. *
  4. * Copyright (c) 2022 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-null.h"
  22. #include "io/channel-watch.h"
  23. #include "qapi/error.h"
  24. #include "trace.h"
  25. #include "qemu/iov.h"
  26. typedef struct QIOChannelNullSource QIOChannelNullSource;
  27. struct QIOChannelNullSource {
  28. GSource parent;
  29. QIOChannel *ioc;
  30. GIOCondition condition;
  31. };
  32. QIOChannelNull *
  33. qio_channel_null_new(void)
  34. {
  35. QIOChannelNull *ioc;
  36. ioc = QIO_CHANNEL_NULL(object_new(TYPE_QIO_CHANNEL_NULL));
  37. trace_qio_channel_null_new(ioc);
  38. return ioc;
  39. }
  40. static void
  41. qio_channel_null_init(Object *obj)
  42. {
  43. QIOChannelNull *ioc = QIO_CHANNEL_NULL(obj);
  44. ioc->closed = false;
  45. }
  46. static ssize_t
  47. qio_channel_null_readv(QIOChannel *ioc,
  48. const struct iovec *iov,
  49. size_t niov,
  50. int **fds G_GNUC_UNUSED,
  51. size_t *nfds G_GNUC_UNUSED,
  52. int flags,
  53. Error **errp)
  54. {
  55. QIOChannelNull *nioc = QIO_CHANNEL_NULL(ioc);
  56. if (nioc->closed) {
  57. error_setg_errno(errp, EINVAL,
  58. "Channel is closed");
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static ssize_t
  64. qio_channel_null_writev(QIOChannel *ioc,
  65. const struct iovec *iov,
  66. size_t niov,
  67. int *fds G_GNUC_UNUSED,
  68. size_t nfds G_GNUC_UNUSED,
  69. int flags G_GNUC_UNUSED,
  70. Error **errp)
  71. {
  72. QIOChannelNull *nioc = QIO_CHANNEL_NULL(ioc);
  73. if (nioc->closed) {
  74. error_setg_errno(errp, EINVAL,
  75. "Channel is closed");
  76. return -1;
  77. }
  78. return iov_size(iov, niov);
  79. }
  80. static int
  81. qio_channel_null_set_blocking(QIOChannel *ioc G_GNUC_UNUSED,
  82. bool enabled G_GNUC_UNUSED,
  83. Error **errp G_GNUC_UNUSED)
  84. {
  85. return 0;
  86. }
  87. static off_t
  88. qio_channel_null_seek(QIOChannel *ioc G_GNUC_UNUSED,
  89. off_t offset G_GNUC_UNUSED,
  90. int whence G_GNUC_UNUSED,
  91. Error **errp G_GNUC_UNUSED)
  92. {
  93. return 0;
  94. }
  95. static int
  96. qio_channel_null_close(QIOChannel *ioc,
  97. Error **errp G_GNUC_UNUSED)
  98. {
  99. QIOChannelNull *nioc = QIO_CHANNEL_NULL(ioc);
  100. nioc->closed = true;
  101. return 0;
  102. }
  103. static void
  104. qio_channel_null_set_aio_fd_handler(QIOChannel *ioc G_GNUC_UNUSED,
  105. AioContext *read_ctx G_GNUC_UNUSED,
  106. IOHandler *io_read G_GNUC_UNUSED,
  107. AioContext *write_ctx G_GNUC_UNUSED,
  108. IOHandler *io_write G_GNUC_UNUSED,
  109. void *opaque G_GNUC_UNUSED)
  110. {
  111. }
  112. static gboolean
  113. qio_channel_null_source_prepare(GSource *source G_GNUC_UNUSED,
  114. gint *timeout)
  115. {
  116. *timeout = -1;
  117. return TRUE;
  118. }
  119. static gboolean
  120. qio_channel_null_source_check(GSource *source G_GNUC_UNUSED)
  121. {
  122. return TRUE;
  123. }
  124. static gboolean
  125. qio_channel_null_source_dispatch(GSource *source,
  126. GSourceFunc callback,
  127. gpointer user_data)
  128. {
  129. QIOChannelFunc func = (QIOChannelFunc)callback;
  130. QIOChannelNullSource *ssource = (QIOChannelNullSource *)source;
  131. return (*func)(ssource->ioc,
  132. ssource->condition,
  133. user_data);
  134. }
  135. static void
  136. qio_channel_null_source_finalize(GSource *source)
  137. {
  138. QIOChannelNullSource *ssource = (QIOChannelNullSource *)source;
  139. object_unref(OBJECT(ssource->ioc));
  140. }
  141. GSourceFuncs qio_channel_null_source_funcs = {
  142. qio_channel_null_source_prepare,
  143. qio_channel_null_source_check,
  144. qio_channel_null_source_dispatch,
  145. qio_channel_null_source_finalize
  146. };
  147. static GSource *
  148. qio_channel_null_create_watch(QIOChannel *ioc,
  149. GIOCondition condition)
  150. {
  151. GSource *source;
  152. QIOChannelNullSource *ssource;
  153. source = g_source_new(&qio_channel_null_source_funcs,
  154. sizeof(QIOChannelNullSource));
  155. ssource = (QIOChannelNullSource *)source;
  156. ssource->ioc = ioc;
  157. object_ref(OBJECT(ioc));
  158. ssource->condition = condition;
  159. return source;
  160. }
  161. static void
  162. qio_channel_null_class_init(ObjectClass *klass,
  163. void *class_data G_GNUC_UNUSED)
  164. {
  165. QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
  166. ioc_klass->io_writev = qio_channel_null_writev;
  167. ioc_klass->io_readv = qio_channel_null_readv;
  168. ioc_klass->io_set_blocking = qio_channel_null_set_blocking;
  169. ioc_klass->io_seek = qio_channel_null_seek;
  170. ioc_klass->io_close = qio_channel_null_close;
  171. ioc_klass->io_create_watch = qio_channel_null_create_watch;
  172. ioc_klass->io_set_aio_fd_handler = qio_channel_null_set_aio_fd_handler;
  173. }
  174. static const TypeInfo qio_channel_null_info = {
  175. .parent = TYPE_QIO_CHANNEL,
  176. .name = TYPE_QIO_CHANNEL_NULL,
  177. .instance_size = sizeof(QIOChannelNull),
  178. .instance_init = qio_channel_null_init,
  179. .class_init = qio_channel_null_class_init,
  180. };
  181. static void
  182. qio_channel_null_register_types(void)
  183. {
  184. type_register_static(&qio_channel_null_info);
  185. }
  186. type_init(qio_channel_null_register_types);