4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qemu/module.h"
27 #include "qemu/option.h"
28 #include "qemu/sockets.h"
29 #include "qapi/error.h"
30 #include "chardev/char.h"
33 #include "chardev/char-win.h"
34 #include "chardev/char-win-stdio.h"
37 #include "chardev/char-fd.h"
41 /* init terminal so that we can grab keys */
42 static struct termios oldtty
;
43 static int old_fd0_flags
;
44 static int old_fd1_flags
;
45 static bool stdio_in_use
;
46 static bool stdio_allow_signal
;
47 static bool stdio_echo_state
;
49 static void term_exit(void)
52 tcsetattr(0, TCSANOW
, &oldtty
);
53 fcntl(0, F_SETFL
, old_fd0_flags
);
54 fcntl(1, F_SETFL
, old_fd1_flags
);
59 static void qemu_chr_set_echo_stdio(Chardev
*chr
, bool echo
)
63 stdio_echo_state
= echo
;
66 tty
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
67 | INLCR
| IGNCR
| ICRNL
| IXON
);
69 tty
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
70 tty
.c_cflag
&= ~(CSIZE
| PARENB
);
75 if (!stdio_allow_signal
) {
79 tcsetattr(0, TCSANOW
, &tty
);
82 static void term_stdio_handler(int sig
)
84 /* restore echo after resume from suspend. */
85 qemu_chr_set_echo_stdio(NULL
, stdio_echo_state
);
88 static void qemu_chr_open_stdio(Chardev
*chr
,
89 ChardevBackend
*backend
,
93 ChardevStdio
*opts
= backend
->u
.stdio
.data
;
96 if (is_daemonized()) {
97 error_setg(errp
, "cannot use stdio with -daemonize");
102 error_setg(errp
, "cannot use stdio by multiple character devices");
107 old_fd0_flags
= fcntl(0, F_GETFL
);
108 old_fd1_flags
= fcntl(1, F_GETFL
);
109 tcgetattr(0, &oldtty
);
110 if (!g_unix_set_fd_nonblocking(0, true, NULL
)) {
111 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
116 memset(&act
, 0, sizeof(act
));
117 act
.sa_handler
= term_stdio_handler
;
118 sigaction(SIGCONT
, &act
, NULL
);
120 qemu_chr_open_fd(chr
, 0, 1);
122 stdio_allow_signal
= !opts
->has_signal
|| opts
->signal
;
123 qemu_chr_set_echo_stdio(chr
, false);
127 static void qemu_chr_parse_stdio(QemuOpts
*opts
, ChardevBackend
*backend
,
132 backend
->type
= CHARDEV_BACKEND_KIND_STDIO
;
133 stdio
= backend
->u
.stdio
.data
= g_new0(ChardevStdio
, 1);
134 qemu_chr_parse_common(opts
, qapi_ChardevStdio_base(stdio
));
135 stdio
->has_signal
= true;
136 stdio
->signal
= qemu_opt_get_bool(opts
, "signal", true);
139 static void char_stdio_class_init(ObjectClass
*oc
, void *data
)
141 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
143 cc
->parse
= qemu_chr_parse_stdio
;
145 cc
->open
= qemu_chr_open_stdio
;
146 cc
->chr_set_echo
= qemu_chr_set_echo_stdio
;
150 static void char_stdio_finalize(Object
*obj
)
157 static const TypeInfo char_stdio_type_info
= {
158 .name
= TYPE_CHARDEV_STDIO
,
160 .parent
= TYPE_CHARDEV_WIN_STDIO
,
162 .parent
= TYPE_CHARDEV_FD
,
164 .instance_finalize
= char_stdio_finalize
,
165 .class_init
= char_stdio_class_init
,
168 static void register_types(void)
170 type_register_static(&char_stdio_type_info
);
173 type_init(register_types
);