hw/arm/aspeed_ast10x0: Map HACE peripheral
[qemu/kevin.git] / chardev / char-pty.c
blob92fd33c854cb36ba12451c6b54c10f33be32bdc1
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.
25 #include "qemu/osdep.h"
26 #include "qapi/error.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/module.h"
32 #include "qemu/qemu-print.h"
34 #include "chardev/char-io.h"
35 #include "qom/object.h"
37 struct PtyChardev {
38 Chardev parent;
39 QIOChannel *ioc;
40 int read_bytes;
42 int connected;
43 GSource *timer_src;
45 typedef struct PtyChardev PtyChardev;
47 DECLARE_INSTANCE_CHECKER(PtyChardev, PTY_CHARDEV,
48 TYPE_CHARDEV_PTY)
50 static void pty_chr_state(Chardev *chr, int connected);
52 static void pty_chr_timer_cancel(PtyChardev *s)
54 if (s->timer_src) {
55 g_source_destroy(s->timer_src);
56 g_source_unref(s->timer_src);
57 s->timer_src = NULL;
61 static gboolean pty_chr_timer(gpointer opaque)
63 struct Chardev *chr = CHARDEV(opaque);
64 PtyChardev *s = PTY_CHARDEV(opaque);
66 pty_chr_timer_cancel(s);
67 if (!s->connected) {
68 /* Next poll ... */
69 qemu_chr_be_update_read_handlers(chr, chr->gcontext);
71 return FALSE;
74 static void pty_chr_rearm_timer(Chardev *chr, int ms)
76 PtyChardev *s = PTY_CHARDEV(chr);
77 char *name;
79 pty_chr_timer_cancel(s);
80 name = g_strdup_printf("pty-timer-%s", chr->label);
81 s->timer_src = qemu_chr_timeout_add_ms(chr, ms, pty_chr_timer, chr);
82 g_source_set_name(s->timer_src, name);
83 g_free(name);
86 static void pty_chr_update_read_handler(Chardev *chr)
88 PtyChardev *s = PTY_CHARDEV(chr);
89 GPollFD pfd;
90 int rc;
91 QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc);
93 pfd.fd = fioc->fd;
94 pfd.events = G_IO_OUT;
95 pfd.revents = 0;
96 rc = RETRY_ON_EINTR(g_poll(&pfd, 1, 0));
97 assert(rc >= 0);
99 if (pfd.revents & G_IO_HUP) {
100 pty_chr_state(chr, 0);
101 } else {
102 pty_chr_state(chr, 1);
106 static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len)
108 PtyChardev *s = PTY_CHARDEV(chr);
110 if (!s->connected) {
111 return len;
113 return io_channel_send(s->ioc, buf, len);
116 static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond)
118 PtyChardev *s = PTY_CHARDEV(chr);
119 if (!s->connected) {
120 return NULL;
122 return qio_channel_create_watch(s->ioc, cond);
125 static int pty_chr_read_poll(void *opaque)
127 Chardev *chr = CHARDEV(opaque);
128 PtyChardev *s = PTY_CHARDEV(opaque);
130 s->read_bytes = qemu_chr_be_can_write(chr);
131 return s->read_bytes;
134 static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
136 Chardev *chr = CHARDEV(opaque);
137 PtyChardev *s = PTY_CHARDEV(opaque);
138 gsize len;
139 uint8_t buf[CHR_READ_BUF_LEN];
140 ssize_t ret;
142 len = sizeof(buf);
143 if (len > s->read_bytes) {
144 len = s->read_bytes;
146 if (len == 0) {
147 return TRUE;
149 ret = qio_channel_read(s->ioc, (char *)buf, len, NULL);
150 if (ret <= 0) {
151 pty_chr_state(chr, 0);
152 return FALSE;
153 } else {
154 pty_chr_state(chr, 1);
155 qemu_chr_be_write(chr, buf, ret);
157 return TRUE;
160 static void pty_chr_state(Chardev *chr, int connected)
162 PtyChardev *s = PTY_CHARDEV(chr);
164 if (!connected) {
165 remove_fd_in_watch(chr);
166 s->connected = 0;
167 /* (re-)connect poll interval for idle guests: once per second.
168 * We check more frequently in case the guests sends data to
169 * the virtual device linked to our pty. */
170 pty_chr_rearm_timer(chr, 1000);
171 } else {
172 pty_chr_timer_cancel(s);
173 if (!s->connected) {
174 s->connected = 1;
175 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
177 if (!chr->gsource) {
178 chr->gsource = io_add_watch_poll(chr, s->ioc,
179 pty_chr_read_poll,
180 pty_chr_read,
181 chr, chr->gcontext);
186 static void char_pty_finalize(Object *obj)
188 Chardev *chr = CHARDEV(obj);
189 PtyChardev *s = PTY_CHARDEV(obj);
191 pty_chr_state(chr, 0);
192 object_unref(OBJECT(s->ioc));
193 pty_chr_timer_cancel(s);
194 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
197 #if defined HAVE_PTY_H
198 # include <pty.h>
199 #elif defined CONFIG_BSD
200 # include <termios.h>
201 # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
202 # include <libutil.h>
203 # else
204 # include <util.h>
205 # endif
206 #elif defined CONFIG_SOLARIS
207 # include <termios.h>
208 # include <stropts.h>
209 #else
210 # include <termios.h>
211 #endif
213 #ifdef __sun__
215 #if !defined(HAVE_OPENPTY)
216 /* Once illumos has openpty(), this is going to be removed. */
217 static int openpty(int *amaster, int *aslave, char *name,
218 struct termios *termp, struct winsize *winp)
220 const char *slave;
221 int mfd = -1, sfd = -1;
223 *amaster = *aslave = -1;
225 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
226 if (mfd < 0) {
227 goto err;
230 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) {
231 goto err;
234 if ((slave = ptsname(mfd)) == NULL) {
235 goto err;
238 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) {
239 goto err;
242 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
243 (termp != NULL && tcgetattr(sfd, termp) < 0)) {
244 goto err;
247 *amaster = mfd;
248 *aslave = sfd;
250 if (winp) {
251 ioctl(sfd, TIOCSWINSZ, winp);
254 return 0;
256 err:
257 if (sfd != -1) {
258 close(sfd);
260 close(mfd);
261 return -1;
263 #endif
265 static void cfmakeraw (struct termios *termios_p)
267 termios_p->c_iflag &=
268 ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
269 termios_p->c_oflag &= ~OPOST;
270 termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
271 termios_p->c_cflag &= ~(CSIZE | PARENB);
272 termios_p->c_cflag |= CS8;
274 termios_p->c_cc[VMIN] = 0;
275 termios_p->c_cc[VTIME] = 0;
277 #endif
279 /* like openpty() but also makes it raw; return master fd */
280 static int qemu_openpty_raw(int *aslave, char *pty_name)
282 int amaster;
283 struct termios tty;
284 #if defined(__OpenBSD__) || defined(__DragonFly__)
285 char pty_buf[PATH_MAX];
286 #define q_ptsname(x) pty_buf
287 #else
288 char *pty_buf = NULL;
289 #define q_ptsname(x) ptsname(x)
290 #endif
292 if (openpty(&amaster, aslave, pty_buf, NULL, NULL) < 0) {
293 return -1;
296 /* Set raw attributes on the pty. */
297 tcgetattr(*aslave, &tty);
298 cfmakeraw(&tty);
299 tcsetattr(*aslave, TCSAFLUSH, &tty);
301 if (pty_name) {
302 strcpy(pty_name, q_ptsname(amaster));
305 return amaster;
308 static void char_pty_open(Chardev *chr,
309 ChardevBackend *backend,
310 bool *be_opened,
311 Error **errp)
313 PtyChardev *s;
314 int master_fd, slave_fd;
315 char pty_name[PATH_MAX];
316 char *name;
318 master_fd = qemu_openpty_raw(&slave_fd, pty_name);
319 if (master_fd < 0) {
320 error_setg_errno(errp, errno, "Failed to create PTY");
321 return;
324 close(slave_fd);
325 if (!g_unix_set_fd_nonblocking(master_fd, true, NULL)) {
326 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
327 return;
330 chr->filename = g_strdup_printf("pty:%s", pty_name);
331 qemu_printf("char device redirected to %s (label %s)\n",
332 pty_name, chr->label);
334 s = PTY_CHARDEV(chr);
335 s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd));
336 name = g_strdup_printf("chardev-pty-%s", chr->label);
337 qio_channel_set_name(QIO_CHANNEL(s->ioc), name);
338 g_free(name);
339 s->timer_src = NULL;
340 *be_opened = false;
343 static void char_pty_class_init(ObjectClass *oc, void *data)
345 ChardevClass *cc = CHARDEV_CLASS(oc);
347 cc->open = char_pty_open;
348 cc->chr_write = char_pty_chr_write;
349 cc->chr_update_read_handler = pty_chr_update_read_handler;
350 cc->chr_add_watch = pty_chr_add_watch;
353 static const TypeInfo char_pty_type_info = {
354 .name = TYPE_CHARDEV_PTY,
355 .parent = TYPE_CHARDEV,
356 .instance_size = sizeof(PtyChardev),
357 .instance_finalize = char_pty_finalize,
358 .class_init = char_pty_class_init,
361 static void register_types(void)
363 type_register_static(&char_pty_type_info);
366 type_init(register_types);