You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

char-pty.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "qapi/error.h"
  26. #include "qemu-common.h"
  27. #include "chardev/char.h"
  28. #include "io/channel-file.h"
  29. #include "qemu/sockets.h"
  30. #include "qemu/error-report.h"
  31. #include "qemu/qemu-print.h"
  32. #include "chardev/char-io.h"
  33. typedef struct {
  34. Chardev parent;
  35. QIOChannel *ioc;
  36. int read_bytes;
  37. int connected;
  38. GSource *timer_src;
  39. } PtyChardev;
  40. #define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY)
  41. static void pty_chr_state(Chardev *chr, int connected);
  42. static void pty_chr_timer_cancel(PtyChardev *s)
  43. {
  44. if (s->timer_src) {
  45. g_source_destroy(s->timer_src);
  46. g_source_unref(s->timer_src);
  47. s->timer_src = NULL;
  48. }
  49. }
  50. static gboolean pty_chr_timer(gpointer opaque)
  51. {
  52. struct Chardev *chr = CHARDEV(opaque);
  53. PtyChardev *s = PTY_CHARDEV(opaque);
  54. pty_chr_timer_cancel(s);
  55. if (!s->connected) {
  56. /* Next poll ... */
  57. qemu_chr_be_update_read_handlers(chr, chr->gcontext);
  58. }
  59. return FALSE;
  60. }
  61. static void pty_chr_rearm_timer(Chardev *chr, int ms)
  62. {
  63. PtyChardev *s = PTY_CHARDEV(chr);
  64. char *name;
  65. pty_chr_timer_cancel(s);
  66. name = g_strdup_printf("pty-timer-%s", chr->label);
  67. s->timer_src = qemu_chr_timeout_add_ms(chr, ms, pty_chr_timer, chr);
  68. g_source_set_name(s->timer_src, name);
  69. g_free(name);
  70. }
  71. static void pty_chr_update_read_handler(Chardev *chr)
  72. {
  73. PtyChardev *s = PTY_CHARDEV(chr);
  74. GPollFD pfd;
  75. int rc;
  76. QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc);
  77. pfd.fd = fioc->fd;
  78. pfd.events = G_IO_OUT;
  79. pfd.revents = 0;
  80. do {
  81. rc = g_poll(&pfd, 1, 0);
  82. } while (rc == -1 && errno == EINTR);
  83. assert(rc >= 0);
  84. if (pfd.revents & G_IO_HUP) {
  85. pty_chr_state(chr, 0);
  86. } else {
  87. pty_chr_state(chr, 1);
  88. }
  89. }
  90. static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len)
  91. {
  92. PtyChardev *s = PTY_CHARDEV(chr);
  93. if (!s->connected) {
  94. return len;
  95. }
  96. return io_channel_send(s->ioc, buf, len);
  97. }
  98. static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond)
  99. {
  100. PtyChardev *s = PTY_CHARDEV(chr);
  101. if (!s->connected) {
  102. return NULL;
  103. }
  104. return qio_channel_create_watch(s->ioc, cond);
  105. }
  106. static int pty_chr_read_poll(void *opaque)
  107. {
  108. Chardev *chr = CHARDEV(opaque);
  109. PtyChardev *s = PTY_CHARDEV(opaque);
  110. s->read_bytes = qemu_chr_be_can_write(chr);
  111. return s->read_bytes;
  112. }
  113. static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
  114. {
  115. Chardev *chr = CHARDEV(opaque);
  116. PtyChardev *s = PTY_CHARDEV(opaque);
  117. gsize len;
  118. uint8_t buf[CHR_READ_BUF_LEN];
  119. ssize_t ret;
  120. len = sizeof(buf);
  121. if (len > s->read_bytes) {
  122. len = s->read_bytes;
  123. }
  124. if (len == 0) {
  125. return TRUE;
  126. }
  127. ret = qio_channel_read(s->ioc, (char *)buf, len, NULL);
  128. if (ret <= 0) {
  129. pty_chr_state(chr, 0);
  130. return FALSE;
  131. } else {
  132. pty_chr_state(chr, 1);
  133. qemu_chr_be_write(chr, buf, ret);
  134. }
  135. return TRUE;
  136. }
  137. static void pty_chr_state(Chardev *chr, int connected)
  138. {
  139. PtyChardev *s = PTY_CHARDEV(chr);
  140. if (!connected) {
  141. remove_fd_in_watch(chr);
  142. s->connected = 0;
  143. /* (re-)connect poll interval for idle guests: once per second.
  144. * We check more frequently in case the guests sends data to
  145. * the virtual device linked to our pty. */
  146. pty_chr_rearm_timer(chr, 1000);
  147. } else {
  148. pty_chr_timer_cancel(s);
  149. if (!s->connected) {
  150. s->connected = 1;
  151. qemu_chr_be_event(chr, CHR_EVENT_OPENED);
  152. }
  153. if (!chr->gsource) {
  154. chr->gsource = io_add_watch_poll(chr, s->ioc,
  155. pty_chr_read_poll,
  156. pty_chr_read,
  157. chr, chr->gcontext);
  158. }
  159. }
  160. }
  161. static void char_pty_finalize(Object *obj)
  162. {
  163. Chardev *chr = CHARDEV(obj);
  164. PtyChardev *s = PTY_CHARDEV(obj);
  165. pty_chr_state(chr, 0);
  166. object_unref(OBJECT(s->ioc));
  167. pty_chr_timer_cancel(s);
  168. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  169. }
  170. static void char_pty_open(Chardev *chr,
  171. ChardevBackend *backend,
  172. bool *be_opened,
  173. Error **errp)
  174. {
  175. PtyChardev *s;
  176. int master_fd, slave_fd;
  177. char pty_name[PATH_MAX];
  178. char *name;
  179. master_fd = qemu_openpty_raw(&slave_fd, pty_name);
  180. if (master_fd < 0) {
  181. error_setg_errno(errp, errno, "Failed to create PTY");
  182. return;
  183. }
  184. close(slave_fd);
  185. qemu_set_nonblock(master_fd);
  186. chr->filename = g_strdup_printf("pty:%s", pty_name);
  187. qemu_printf("char device redirected to %s (label %s)\n",
  188. pty_name, chr->label);
  189. s = PTY_CHARDEV(chr);
  190. s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd));
  191. name = g_strdup_printf("chardev-pty-%s", chr->label);
  192. qio_channel_set_name(QIO_CHANNEL(s->ioc), name);
  193. g_free(name);
  194. s->timer_src = NULL;
  195. *be_opened = false;
  196. }
  197. static void char_pty_class_init(ObjectClass *oc, void *data)
  198. {
  199. ChardevClass *cc = CHARDEV_CLASS(oc);
  200. cc->open = char_pty_open;
  201. cc->chr_write = char_pty_chr_write;
  202. cc->chr_update_read_handler = pty_chr_update_read_handler;
  203. cc->chr_add_watch = pty_chr_add_watch;
  204. }
  205. static const TypeInfo char_pty_type_info = {
  206. .name = TYPE_CHARDEV_PTY,
  207. .parent = TYPE_CHARDEV,
  208. .instance_size = sizeof(PtyChardev),
  209. .instance_finalize = char_pty_finalize,
  210. .class_init = char_pty_class_init,
  211. };
  212. static void register_types(void)
  213. {
  214. type_register_static(&char_pty_type_info);
  215. }
  216. type_init(register_types);