1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
41static struct termios oldtty;
42static int old_fd0_flags;
43static bool stdio_in_use;
44static bool stdio_allow_signal;
45static bool stdio_echo_state;
46
47static void term_exit(void)
48{
49 if (stdio_in_use) {
50 tcsetattr(0, TCSANOW, &oldtty);
51 fcntl(0, F_SETFL, old_fd0_flags);
52 }
53}
54
55static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo)
56{
57 struct termios tty;
58
59 stdio_echo_state = echo;
60 tty = oldtty;
61 if (!echo) {
62 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
63 | INLCR | IGNCR | ICRNL | IXON);
64 tty.c_oflag |= OPOST;
65 tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
66 tty.c_cflag &= ~(CSIZE | PARENB);
67 tty.c_cflag |= CS8;
68 tty.c_cc[VMIN] = 1;
69 tty.c_cc[VTIME] = 0;
70 }
71 if (!stdio_allow_signal) {
72 tty.c_lflag &= ~ISIG;
73 }
74
75 tcsetattr(0, TCSANOW, &tty);
76}
77
78static void term_stdio_handler(int sig)
79{
80
81 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
82}
83
84static void qemu_chr_open_stdio(Chardev *chr,
85 ChardevBackend *backend,
86 bool *be_opened,
87 Error **errp)
88{
89 ChardevStdio *opts = backend->u.stdio.data;
90 struct sigaction act;
91
92 if (is_daemonized()) {
93 error_setg(errp, "cannot use stdio with -daemonize");
94 return;
95 }
96
97 if (stdio_in_use) {
98 error_setg(errp, "cannot use stdio by multiple character devices");
99 return;
100 }
101
102 stdio_in_use = true;
103 old_fd0_flags = fcntl(0, F_GETFL);
104 tcgetattr(0, &oldtty);
105 qemu_set_nonblock(0);
106 atexit(term_exit);
107
108 memset(&act, 0, sizeof(act));
109 act.sa_handler = term_stdio_handler;
110 sigaction(SIGCONT, &act, NULL);
111
112 qemu_chr_open_fd(chr, 0, 1);
113
114 if (opts->has_signal) {
115 stdio_allow_signal = opts->signal;
116 }
117 qemu_chr_set_echo_stdio(chr, false);
118}
119#endif
120
121static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
122 Error **errp)
123{
124 ChardevStdio *stdio;
125
126 backend->type = CHARDEV_BACKEND_KIND_STDIO;
127 stdio = backend->u.stdio.data = g_new0(ChardevStdio, 1);
128 qemu_chr_parse_common(opts, qapi_ChardevStdio_base(stdio));
129 stdio->has_signal = true;
130 stdio->signal = qemu_opt_get_bool(opts, "signal", true);
131}
132
133static void char_stdio_class_init(ObjectClass *oc, void *data)
134{
135 ChardevClass *cc = CHARDEV_CLASS(oc);
136
137 cc->parse = qemu_chr_parse_stdio;
138#ifndef _WIN32
139 cc->open = qemu_chr_open_stdio;
140 cc->chr_set_echo = qemu_chr_set_echo_stdio;
141#endif
142}
143
144static void char_stdio_finalize(Object *obj)
145{
146#ifndef _WIN32
147 term_exit();
148#endif
149}
150
151static const TypeInfo char_stdio_type_info = {
152 .name = TYPE_CHARDEV_STDIO,
153#ifdef _WIN32
154 .parent = TYPE_CHARDEV_WIN_STDIO,
155#else
156 .parent = TYPE_CHARDEV_FD,
157#endif
158 .instance_finalize = char_stdio_finalize,
159 .class_init = char_stdio_class_init,
160};
161
162static void register_types(void)
163{
164 type_register_static(&char_stdio_type_info);
165}
166
167type_init(register_types);
168