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