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 25 #include "qemu/osdep.h" 26 #include "qemu/option.h" 27 #include "qemu/sockets.h" 28 #include "qapi/error.h" 29 #include "chardev/char.h" 30 31 #ifdef _WIN32 32 #include "chardev/char-win.h" 33 #include "chardev/char-win-stdio.h" 34 #else 35 #include <termios.h> 36 #include "chardev/char-fd.h" 37 #endif 38 39 #ifndef _WIN32 40 /* init terminal so that we can grab keys */ 41 static struct termios oldtty; 42 static int old_fd0_flags; 43 static bool stdio_in_use; 44 static bool stdio_allow_signal; 45 static bool stdio_echo_state; 46 47 static void term_exit(void) 48 { 49 tcsetattr(0, TCSANOW, &oldtty); 50 fcntl(0, F_SETFL, old_fd0_flags); 51 } 52 53 static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo) 54 { 55 struct termios tty; 56 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 73 tcsetattr(0, TCSANOW, &tty); 74 } 75 76 static void term_stdio_handler(int sig) 77 { 78 /* restore echo after resume from suspend. */ 79 qemu_chr_set_echo_stdio(NULL, stdio_echo_state); 80 } 81 82 static void qemu_chr_open_stdio(Chardev *chr, 83 ChardevBackend *backend, 84 bool *be_opened, 85 Error **errp) 86 { 87 ChardevStdio *opts = backend->u.stdio.data; 88 struct sigaction act; 89 90 if (is_daemonized()) { 91 error_setg(errp, "cannot use stdio with -daemonize"); 92 return; 93 } 94 95 if (stdio_in_use) { 96 error_setg(errp, "cannot use stdio by multiple character devices"); 97 return; 98 } 99 100 stdio_in_use = true; 101 old_fd0_flags = fcntl(0, F_GETFL); 102 tcgetattr(0, &oldtty); 103 qemu_set_nonblock(0); 104 atexit(term_exit); 105 106 memset(&act, 0, sizeof(act)); 107 act.sa_handler = term_stdio_handler; 108 sigaction(SIGCONT, &act, NULL); 109 110 qemu_chr_open_fd(chr, 0, 1); 111 112 if (opts->has_signal) { 113 stdio_allow_signal = opts->signal; 114 } 115 qemu_chr_set_echo_stdio(chr, false); 116 } 117 #endif 118 119 static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, 120 Error **errp) 121 { 122 ChardevStdio *stdio; 123 124 backend->type = CHARDEV_BACKEND_KIND_STDIO; 125 stdio = backend->u.stdio.data = g_new0(ChardevStdio, 1); 126 qemu_chr_parse_common(opts, qapi_ChardevStdio_base(stdio)); 127 stdio->has_signal = true; 128 stdio->signal = qemu_opt_get_bool(opts, "signal", true); 129 } 130 131 static void char_stdio_class_init(ObjectClass *oc, void *data) 132 { 133 ChardevClass *cc = CHARDEV_CLASS(oc); 134 135 cc->parse = qemu_chr_parse_stdio; 136 #ifndef _WIN32 137 cc->open = qemu_chr_open_stdio; 138 cc->chr_set_echo = qemu_chr_set_echo_stdio; 139 #endif 140 } 141 142 static void char_stdio_finalize(Object *obj) 143 { 144 #ifndef _WIN32 145 term_exit(); 146 #endif 147 } 148 149 static const TypeInfo char_stdio_type_info = { 150 .name = TYPE_CHARDEV_STDIO, 151 #ifdef _WIN32 152 .parent = TYPE_CHARDEV_WIN_STDIO, 153 #else 154 .parent = TYPE_CHARDEV_FD, 155 #endif 156 .instance_finalize = char_stdio_finalize, 157 .class_init = char_stdio_class_init, 158 }; 159 160 static void register_types(void) 161 { 162 type_register_static(&char_stdio_type_info); 163 } 164 165 type_init(register_types); 166