blob: 48db8d2f30fcf0b481c79ea69aab720454596a05 [file] [log] [blame]
Marc-André Lureaud1800812016-12-12 18:59:04 +03001/*
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 */
Markus Armbruster922a01a2018-02-01 12:18:46 +010024
Marc-André Lureaud1800812016-12-12 18:59:04 +030025#include "qemu/osdep.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020026#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010027#include "qemu/option.h"
Marc-André Lureaud1800812016-12-12 18:59:04 +030028#include "qemu/sockets.h"
29#include "qapi/error.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040030#include "chardev/char.h"
Marc-André Lureaud1800812016-12-12 18:59:04 +030031
32#ifdef _WIN32
Marc-André Lureau8228e352017-01-26 17:19:46 +040033#include "chardev/char-win.h"
34#include "chardev/char-win-stdio.h"
Marc-André Lureaud1800812016-12-12 18:59:04 +030035#else
36#include <termios.h>
Marc-André Lureau8228e352017-01-26 17:19:46 +040037#include "chardev/char-fd.h"
Marc-André Lureaud1800812016-12-12 18:59:04 +030038#endif
39
40#ifndef _WIN32
41/* init terminal so that we can grab keys */
42static struct termios oldtty;
43static int old_fd0_flags;
Maxim Mikityanskiya0124e32024-07-03 22:08:12 +030044static int old_fd1_flags;
Marc-André Lureaud1800812016-12-12 18:59:04 +030045static bool stdio_in_use;
46static bool stdio_allow_signal;
47static bool stdio_echo_state;
48
49static void term_exit(void)
50{
Daniel P. Berrangé1507bd12018-06-04 13:30:43 +010051 if (stdio_in_use) {
52 tcsetattr(0, TCSANOW, &oldtty);
53 fcntl(0, F_SETFL, old_fd0_flags);
Maxim Mikityanskiya0124e32024-07-03 22:08:12 +030054 fcntl(1, F_SETFL, old_fd1_flags);
55 stdio_in_use = false;
Daniel P. Berrangé1507bd12018-06-04 13:30:43 +010056 }
Marc-André Lureaud1800812016-12-12 18:59:04 +030057}
58
59static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo)
60{
61 struct termios tty;
62
63 stdio_echo_state = echo;
64 tty = oldtty;
65 if (!echo) {
66 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
67 | INLCR | IGNCR | ICRNL | IXON);
Philippe Mathieu-Daudéed6b0182018-06-07 18:08:18 -030068 tty.c_oflag |= OPOST;
Marc-André Lureaud1800812016-12-12 18:59:04 +030069 tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
70 tty.c_cflag &= ~(CSIZE | PARENB);
71 tty.c_cflag |= CS8;
72 tty.c_cc[VMIN] = 1;
73 tty.c_cc[VTIME] = 0;
74 }
75 if (!stdio_allow_signal) {
76 tty.c_lflag &= ~ISIG;
77 }
78
79 tcsetattr(0, TCSANOW, &tty);
80}
81
82static void term_stdio_handler(int sig)
83{
84 /* restore echo after resume from suspend. */
85 qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
86}
87
88static void qemu_chr_open_stdio(Chardev *chr,
89 ChardevBackend *backend,
90 bool *be_opened,
91 Error **errp)
92{
93 ChardevStdio *opts = backend->u.stdio.data;
94 struct sigaction act;
95
96 if (is_daemonized()) {
97 error_setg(errp, "cannot use stdio with -daemonize");
98 return;
99 }
100
101 if (stdio_in_use) {
102 error_setg(errp, "cannot use stdio by multiple character devices");
103 return;
104 }
105
106 stdio_in_use = true;
107 old_fd0_flags = fcntl(0, F_GETFL);
Maxim Mikityanskiya0124e32024-07-03 22:08:12 +0300108 old_fd1_flags = fcntl(1, F_GETFL);
Marc-André Lureaud1800812016-12-12 18:59:04 +0300109 tcgetattr(0, &oldtty);
Marc-André Lureaub84bb4d2022-04-25 17:39:06 +0400110 if (!g_unix_set_fd_nonblocking(0, true, NULL)) {
111 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
112 return;
113 }
Marc-André Lureaud1800812016-12-12 18:59:04 +0300114 atexit(term_exit);
115
116 memset(&act, 0, sizeof(act));
117 act.sa_handler = term_stdio_handler;
118 sigaction(SIGCONT, &act, NULL);
119
120 qemu_chr_open_fd(chr, 0, 1);
121
Kevin Wolf6deb20f2020-10-23 12:12:17 +0200122 stdio_allow_signal = !opts->has_signal || opts->signal;
Marc-André Lureaud1800812016-12-12 18:59:04 +0300123 qemu_chr_set_echo_stdio(chr, false);
124}
125#endif
126
127static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
128 Error **errp)
129{
130 ChardevStdio *stdio;
131
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);
137}
138
Philippe Mathieu-Daudé12d1a762025-02-09 23:47:35 +0100139static void char_stdio_class_init(ObjectClass *oc, const void *data)
Marc-André Lureaud1800812016-12-12 18:59:04 +0300140{
141 ChardevClass *cc = CHARDEV_CLASS(oc);
142
143 cc->parse = qemu_chr_parse_stdio;
144#ifndef _WIN32
145 cc->open = qemu_chr_open_stdio;
146 cc->chr_set_echo = qemu_chr_set_echo_stdio;
147#endif
148}
149
150static void char_stdio_finalize(Object *obj)
151{
152#ifndef _WIN32
153 term_exit();
154#endif
155}
156
157static const TypeInfo char_stdio_type_info = {
158 .name = TYPE_CHARDEV_STDIO,
159#ifdef _WIN32
160 .parent = TYPE_CHARDEV_WIN_STDIO,
161#else
162 .parent = TYPE_CHARDEV_FD,
163#endif
164 .instance_finalize = char_stdio_finalize,
165 .class_init = char_stdio_class_init,
166};
167
168static void register_types(void)
169{
170 type_register_static(&char_stdio_type_info);
171}
172
173type_init(register_types);