s390x/cpumodel: add gen15 defintions
[qemu.git] / chardev / char-pty.c
blob04759b0ef9031437ddfa5a9230861972abb63d22
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
27 #include "chardev/char.h"
28 #include "io/channel-file.h"
29 #include "qemu/sockets.h"
30 #include "qemu/error-report.h"
31 #include "qemu/qemu-print.h"
33 #include "chardev/char-io.h"
35 typedef struct {
36 Chardev parent;
37 QIOChannel *ioc;
38 int read_bytes;
40 int connected;
41 GSource *timer_src;
42 } PtyChardev;
44 #define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY)
46 static void pty_chr_state(Chardev *chr, int connected);
48 static void pty_chr_timer_cancel(PtyChardev *s)
50 if (s->timer_src) {
51 g_source_destroy(s->timer_src);
52 g_source_unref(s->timer_src);
53 s->timer_src = NULL;
57 static gboolean pty_chr_timer(gpointer opaque)
59 struct Chardev *chr = CHARDEV(opaque);
60 PtyChardev *s = PTY_CHARDEV(opaque);
62 pty_chr_timer_cancel(s);
63 if (!s->connected) {
64 /* Next poll ... */
65 qemu_chr_be_update_read_handlers(chr, chr->gcontext);
67 return FALSE;
70 static void pty_chr_rearm_timer(Chardev *chr, int ms)
72 PtyChardev *s = PTY_CHARDEV(chr);
73 char *name;
75 pty_chr_timer_cancel(s);
76 name = g_strdup_printf("pty-timer-%s", chr->label);
77 s->timer_src = qemu_chr_timeout_add_ms(chr, ms, pty_chr_timer, chr);
78 g_source_set_name(s->timer_src, name);
79 g_free(name);
82 static void pty_chr_update_read_handler(Chardev *chr)
84 PtyChardev *s = PTY_CHARDEV(chr);
85 GPollFD pfd;
86 int rc;
87 QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc);
89 pfd.fd = fioc->fd;
90 pfd.events = G_IO_OUT;
91 pfd.revents = 0;
92 do {
93 rc = g_poll(&pfd, 1, 0);
94 } while (rc == -1 && errno == EINTR);
95 assert(rc >= 0);
97 if (pfd.revents & G_IO_HUP) {
98 pty_chr_state(chr, 0);
99 } else {
100 pty_chr_state(chr, 1);
104 static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len)
106 PtyChardev *s = PTY_CHARDEV(chr);
108 if (!s->connected) {
109 return len;
111 return io_channel_send(s->ioc, buf, len);
114 static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond)
116 PtyChardev *s = PTY_CHARDEV(chr);
117 if (!s->connected) {
118 return NULL;
120 return qio_channel_create_watch(s->ioc, cond);
123 static int pty_chr_read_poll(void *opaque)
125 Chardev *chr = CHARDEV(opaque);
126 PtyChardev *s = PTY_CHARDEV(opaque);
128 s->read_bytes = qemu_chr_be_can_write(chr);
129 return s->read_bytes;
132 static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
134 Chardev *chr = CHARDEV(opaque);
135 PtyChardev *s = PTY_CHARDEV(opaque);
136 gsize len;
137 uint8_t buf[CHR_READ_BUF_LEN];
138 ssize_t ret;
140 len = sizeof(buf);
141 if (len > s->read_bytes) {
142 len = s->read_bytes;
144 if (len == 0) {
145 return TRUE;
147 ret = qio_channel_read(s->ioc, (char *)buf, len, NULL);
148 if (ret <= 0) {
149 pty_chr_state(chr, 0);
150 return FALSE;
151 } else {
152 pty_chr_state(chr, 1);
153 qemu_chr_be_write(chr, buf, ret);
155 return TRUE;
158 static void pty_chr_state(Chardev *chr, int connected)
160 PtyChardev *s = PTY_CHARDEV(chr);
162 if (!connected) {
163 remove_fd_in_watch(chr);
164 s->connected = 0;
165 /* (re-)connect poll interval for idle guests: once per second.
166 * We check more frequently in case the guests sends data to
167 * the virtual device linked to our pty. */
168 pty_chr_rearm_timer(chr, 1000);
169 } else {
170 pty_chr_timer_cancel(s);
171 if (!s->connected) {
172 s->connected = 1;
173 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
175 if (!chr->gsource) {
176 chr->gsource = io_add_watch_poll(chr, s->ioc,
177 pty_chr_read_poll,
178 pty_chr_read,
179 chr, chr->gcontext);
184 static void char_pty_finalize(Object *obj)
186 Chardev *chr = CHARDEV(obj);
187 PtyChardev *s = PTY_CHARDEV(obj);
189 pty_chr_state(chr, 0);
190 object_unref(OBJECT(s->ioc));
191 pty_chr_timer_cancel(s);
192 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
195 static void char_pty_open(Chardev *chr,
196 ChardevBackend *backend,
197 bool *be_opened,
198 Error **errp)
200 PtyChardev *s;
201 int master_fd, slave_fd;
202 char pty_name[PATH_MAX];
203 char *name;
205 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
206 if (master_fd < 0) {
207 error_setg_errno(errp, errno, "Failed to create PTY");
208 return;
211 close(slave_fd);
212 qemu_set_nonblock(master_fd);
214 chr->filename = g_strdup_printf("pty:%s", pty_name);
215 qemu_printf("char device redirected to %s (label %s)\n",
216 pty_name, chr->label);
218 s = PTY_CHARDEV(chr);
219 s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd));
220 name = g_strdup_printf("chardev-pty-%s", chr->label);
221 qio_channel_set_name(QIO_CHANNEL(s->ioc), name);
222 g_free(name);
223 s->timer_src = NULL;
224 *be_opened = false;
227 static void char_pty_class_init(ObjectClass *oc, void *data)
229 ChardevClass *cc = CHARDEV_CLASS(oc);
231 cc->open = char_pty_open;
232 cc->chr_write = char_pty_chr_write;
233 cc->chr_update_read_handler = pty_chr_update_read_handler;
234 cc->chr_add_watch = pty_chr_add_watch;
237 static const TypeInfo char_pty_type_info = {
238 .name = TYPE_CHARDEV_PTY,
239 .parent = TYPE_CHARDEV,
240 .instance_size = sizeof(PtyChardev),
241 .instance_finalize = char_pty_finalize,
242 .class_init = char_pty_class_init,
245 static void register_types(void)
247 type_register_static(&char_pty_type_info);
250 type_init(register_types);