2
0

char-stdio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/option.h"
  27. #include "qemu/sockets.h"
  28. #include "qapi/error.h"
  29. #include "chardev/char.h"
  30. #ifdef _WIN32
  31. #include "chardev/char-win.h"
  32. #include "chardev/char-win-stdio.h"
  33. #else
  34. #include <termios.h>
  35. #include "chardev/char-fd.h"
  36. #endif
  37. #ifndef _WIN32
  38. /* init terminal so that we can grab keys */
  39. static struct termios oldtty;
  40. static int old_fd0_flags;
  41. static int old_fd1_flags;
  42. static bool stdio_in_use;
  43. static bool stdio_allow_signal;
  44. static bool stdio_echo_state;
  45. static void term_exit(void)
  46. {
  47. if (stdio_in_use) {
  48. tcsetattr(0, TCSANOW, &oldtty);
  49. fcntl(0, F_SETFL, old_fd0_flags);
  50. fcntl(1, F_SETFL, old_fd1_flags);
  51. stdio_in_use = false;
  52. }
  53. }
  54. static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo)
  55. {
  56. struct termios tty;
  57. stdio_echo_state = echo;
  58. tty = oldtty;
  59. if (!echo) {
  60. tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
  61. | INLCR | IGNCR | ICRNL | IXON);
  62. tty.c_oflag |= OPOST;
  63. tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
  64. tty.c_cflag &= ~(CSIZE | PARENB);
  65. tty.c_cflag |= CS8;
  66. tty.c_cc[VMIN] = 1;
  67. tty.c_cc[VTIME] = 0;
  68. }
  69. if (!stdio_allow_signal) {
  70. tty.c_lflag &= ~ISIG;
  71. }
  72. tcsetattr(0, TCSANOW, &tty);
  73. }
  74. static void term_stdio_handler(int sig)
  75. {
  76. /* restore echo after resume from suspend. */
  77. qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
  78. }
  79. static void qemu_chr_open_stdio(Chardev *chr,
  80. ChardevBackend *backend,
  81. bool *be_opened,
  82. Error **errp)
  83. {
  84. ChardevStdio *opts = backend->u.stdio.data;
  85. struct sigaction act;
  86. if (is_daemonized()) {
  87. error_setg(errp, "cannot use stdio with -daemonize");
  88. return;
  89. }
  90. if (stdio_in_use) {
  91. error_setg(errp, "cannot use stdio by multiple character devices");
  92. return;
  93. }
  94. stdio_in_use = true;
  95. old_fd0_flags = fcntl(0, F_GETFL);
  96. old_fd1_flags = fcntl(1, F_GETFL);
  97. tcgetattr(0, &oldtty);
  98. if (!g_unix_set_fd_nonblocking(0, true, NULL)) {
  99. error_setg_errno(errp, errno, "Failed to set FD nonblocking");
  100. return;
  101. }
  102. atexit(term_exit);
  103. memset(&act, 0, sizeof(act));
  104. act.sa_handler = term_stdio_handler;
  105. sigaction(SIGCONT, &act, NULL);
  106. qemu_chr_open_fd(chr, 0, 1);
  107. stdio_allow_signal = !opts->has_signal || opts->signal;
  108. qemu_chr_set_echo_stdio(chr, false);
  109. }
  110. #endif
  111. static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
  112. Error **errp)
  113. {
  114. ChardevStdio *stdio;
  115. backend->type = CHARDEV_BACKEND_KIND_STDIO;
  116. stdio = backend->u.stdio.data = g_new0(ChardevStdio, 1);
  117. qemu_chr_parse_common(opts, qapi_ChardevStdio_base(stdio));
  118. stdio->has_signal = true;
  119. stdio->signal = qemu_opt_get_bool(opts, "signal", true);
  120. }
  121. static void char_stdio_class_init(ObjectClass *oc, void *data)
  122. {
  123. ChardevClass *cc = CHARDEV_CLASS(oc);
  124. cc->parse = qemu_chr_parse_stdio;
  125. #ifndef _WIN32
  126. cc->open = qemu_chr_open_stdio;
  127. cc->chr_set_echo = qemu_chr_set_echo_stdio;
  128. #endif
  129. }
  130. static void char_stdio_finalize(Object *obj)
  131. {
  132. #ifndef _WIN32
  133. term_exit();
  134. #endif
  135. }
  136. static const TypeInfo char_stdio_type_info = {
  137. .name = TYPE_CHARDEV_STDIO,
  138. #ifdef _WIN32
  139. .parent = TYPE_CHARDEV_WIN_STDIO,
  140. #else
  141. .parent = TYPE_CHARDEV_FD,
  142. #endif
  143. .instance_finalize = char_stdio_finalize,
  144. .class_init = char_stdio_class_init,
  145. };
  146. static void register_types(void)
  147. {
  148. type_register_static(&char_stdio_type_info);
  149. }
  150. type_init(register_types);