char-fd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/module.h"
  26. #include "qemu/sockets.h"
  27. #include "qapi/error.h"
  28. #include "chardev/char.h"
  29. #include "chardev/char-fe.h"
  30. #include "io/channel-file.h"
  31. #include "chardev/char-fd.h"
  32. #include "chardev/char-io.h"
  33. /* Called with chr_write_lock held. */
  34. static int fd_chr_write(Chardev *chr, const uint8_t *buf, int len)
  35. {
  36. FDChardev *s = FD_CHARDEV(chr);
  37. if (!s->ioc_out) {
  38. return -1;
  39. }
  40. return io_channel_send(s->ioc_out, buf, len);
  41. }
  42. static gboolean fd_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
  43. {
  44. Chardev *chr = CHARDEV(opaque);
  45. FDChardev *s = FD_CHARDEV(opaque);
  46. int len;
  47. uint8_t buf[CHR_READ_BUF_LEN];
  48. ssize_t ret;
  49. len = sizeof(buf);
  50. if (len > s->max_size) {
  51. len = s->max_size;
  52. }
  53. if (len == 0) {
  54. return TRUE;
  55. }
  56. ret = qio_channel_read(
  57. chan, (gchar *)buf, len, NULL);
  58. if (ret == 0) {
  59. remove_fd_in_watch(chr);
  60. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  61. return FALSE;
  62. }
  63. if (ret > 0) {
  64. qemu_chr_be_write(chr, buf, ret);
  65. }
  66. return TRUE;
  67. }
  68. static int fd_chr_read_poll(void *opaque)
  69. {
  70. Chardev *chr = CHARDEV(opaque);
  71. FDChardev *s = FD_CHARDEV(opaque);
  72. s->max_size = qemu_chr_be_can_write(chr);
  73. return s->max_size;
  74. }
  75. typedef struct FDSource {
  76. GSource parent;
  77. GIOCondition cond;
  78. } FDSource;
  79. static gboolean
  80. fd_source_prepare(GSource *source,
  81. gint *timeout_)
  82. {
  83. FDSource *src = (FDSource *)source;
  84. return src->cond != 0;
  85. }
  86. static gboolean
  87. fd_source_check(GSource *source)
  88. {
  89. FDSource *src = (FDSource *)source;
  90. return src->cond != 0;
  91. }
  92. static gboolean
  93. fd_source_dispatch(GSource *source, GSourceFunc callback,
  94. gpointer user_data)
  95. {
  96. FDSource *src = (FDSource *)source;
  97. FEWatchFunc func = (FEWatchFunc)callback;
  98. gboolean ret = G_SOURCE_CONTINUE;
  99. if (src->cond) {
  100. ret = func(NULL, src->cond, user_data);
  101. src->cond = 0;
  102. }
  103. return ret;
  104. }
  105. static GSourceFuncs fd_source_funcs = {
  106. fd_source_prepare,
  107. fd_source_check,
  108. fd_source_dispatch,
  109. NULL, NULL, NULL
  110. };
  111. static GSource *fd_source_new(FDChardev *chr)
  112. {
  113. return g_source_new(&fd_source_funcs, sizeof(FDSource));
  114. }
  115. static gboolean child_func(GIOChannel *source,
  116. GIOCondition condition,
  117. gpointer data)
  118. {
  119. FDSource *parent = data;
  120. parent->cond |= condition;
  121. return G_SOURCE_CONTINUE;
  122. }
  123. static GSource *fd_chr_add_watch(Chardev *chr, GIOCondition cond)
  124. {
  125. FDChardev *s = FD_CHARDEV(chr);
  126. g_autoptr(GSource) source = fd_source_new(s);
  127. if (s->ioc_out) {
  128. g_autoptr(GSource) child = qio_channel_create_watch(s->ioc_out, cond & ~G_IO_IN);
  129. g_source_set_callback(child, (GSourceFunc)child_func, source, NULL);
  130. g_source_add_child_source(source, child);
  131. }
  132. if (s->ioc_in) {
  133. g_autoptr(GSource) child = qio_channel_create_watch(s->ioc_in, cond & ~G_IO_OUT);
  134. g_source_set_callback(child, (GSourceFunc)child_func, source, NULL);
  135. g_source_add_child_source(source, child);
  136. }
  137. return g_steal_pointer(&source);
  138. }
  139. static void fd_chr_update_read_handler(Chardev *chr)
  140. {
  141. FDChardev *s = FD_CHARDEV(chr);
  142. remove_fd_in_watch(chr);
  143. if (s->ioc_in) {
  144. chr->gsource = io_add_watch_poll(chr, s->ioc_in,
  145. fd_chr_read_poll,
  146. fd_chr_read, chr,
  147. chr->gcontext);
  148. }
  149. }
  150. static void char_fd_finalize(Object *obj)
  151. {
  152. Chardev *chr = CHARDEV(obj);
  153. FDChardev *s = FD_CHARDEV(obj);
  154. remove_fd_in_watch(chr);
  155. if (s->ioc_in) {
  156. object_unref(OBJECT(s->ioc_in));
  157. }
  158. if (s->ioc_out) {
  159. object_unref(OBJECT(s->ioc_out));
  160. }
  161. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  162. }
  163. int qmp_chardev_open_file_source(char *src, int flags, Error **errp)
  164. {
  165. int fd = -1;
  166. fd = RETRY_ON_EINTR(qemu_open_old(src, flags, 0666));
  167. if (fd == -1) {
  168. error_setg_file_open(errp, errno, src);
  169. }
  170. return fd;
  171. }
  172. /* open a character device to a unix fd */
  173. void qemu_chr_open_fd(Chardev *chr,
  174. int fd_in, int fd_out)
  175. {
  176. FDChardev *s = FD_CHARDEV(chr);
  177. g_autofree char *name = NULL;
  178. if (fd_out >= 0 && !g_unix_set_fd_nonblocking(fd_out, true, NULL)) {
  179. assert(!"Failed to set FD nonblocking");
  180. }
  181. if (fd_out == fd_in && fd_in >= 0) {
  182. s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
  183. name = g_strdup_printf("chardev-file-%s", chr->label);
  184. qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
  185. s->ioc_out = QIO_CHANNEL(object_ref(s->ioc_in));
  186. return;
  187. }
  188. if (fd_in >= 0) {
  189. s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
  190. name = g_strdup_printf("chardev-file-in-%s", chr->label);
  191. qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
  192. }
  193. if (fd_out >= 0) {
  194. s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out));
  195. g_free(name);
  196. name = g_strdup_printf("chardev-file-out-%s", chr->label);
  197. qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name);
  198. }
  199. }
  200. static void char_fd_class_init(ObjectClass *oc, void *data)
  201. {
  202. ChardevClass *cc = CHARDEV_CLASS(oc);
  203. cc->chr_add_watch = fd_chr_add_watch;
  204. cc->chr_write = fd_chr_write;
  205. cc->chr_update_read_handler = fd_chr_update_read_handler;
  206. }
  207. static const TypeInfo char_fd_type_info = {
  208. .name = TYPE_CHARDEV_FD,
  209. .parent = TYPE_CHARDEV,
  210. .instance_size = sizeof(FDChardev),
  211. .instance_finalize = char_fd_finalize,
  212. .class_init = char_fd_class_init,
  213. .abstract = true,
  214. };
  215. static void register_types(void)
  216. {
  217. type_register_static(&char_fd_type_info);
  218. }
  219. type_init(register_types);