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 "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/option.h"
33 #include "qemu/qemu-print.h"
35 #include "chardev/char-io.h"
36 #include "qom/object.h"
47 typedef struct PtyChardev PtyChardev
;
49 DECLARE_INSTANCE_CHECKER(PtyChardev
, PTY_CHARDEV
,
52 static void pty_chr_state(Chardev
*chr
, int connected
);
54 static void pty_chr_timer_cancel(PtyChardev
*s
)
57 g_source_destroy(s
->timer_src
);
58 g_source_unref(s
->timer_src
);
63 static gboolean
pty_chr_timer(gpointer opaque
)
65 struct Chardev
*chr
= CHARDEV(opaque
);
66 PtyChardev
*s
= PTY_CHARDEV(opaque
);
68 pty_chr_timer_cancel(s
);
71 qemu_chr_be_update_read_handlers(chr
, chr
->gcontext
);
76 static void pty_chr_rearm_timer(Chardev
*chr
, int ms
)
78 PtyChardev
*s
= PTY_CHARDEV(chr
);
81 pty_chr_timer_cancel(s
);
82 name
= g_strdup_printf("pty-timer-%s", chr
->label
);
83 s
->timer_src
= qemu_chr_timeout_add_ms(chr
, ms
, pty_chr_timer
, chr
);
84 g_source_set_name(s
->timer_src
, name
);
88 static void pty_chr_update_read_handler(Chardev
*chr
)
90 PtyChardev
*s
= PTY_CHARDEV(chr
);
93 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(s
->ioc
);
96 pfd
.events
= G_IO_OUT
;
98 rc
= RETRY_ON_EINTR(g_poll(&pfd
, 1, 0));
101 if (pfd
.revents
& G_IO_HUP
) {
102 pty_chr_state(chr
, 0);
104 pty_chr_state(chr
, 1);
108 static int char_pty_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
110 PtyChardev
*s
= PTY_CHARDEV(chr
);
115 return io_channel_send(s
->ioc
, buf
, len
);
119 * The other side might already be re-connected, but the timer might
120 * not have fired yet. So let's check here whether we can write again:
122 pfd
.fd
= QIO_CHANNEL_FILE(s
->ioc
)->fd
;
123 pfd
.events
= G_IO_OUT
;
125 rc
= RETRY_ON_EINTR(g_poll(&pfd
, 1, 0));
127 if (!(pfd
.revents
& G_IO_HUP
) && (pfd
.revents
& G_IO_OUT
)) {
128 io_channel_send(s
->ioc
, buf
, len
);
134 static GSource
*pty_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
136 PtyChardev
*s
= PTY_CHARDEV(chr
);
140 return qio_channel_create_watch(s
->ioc
, cond
);
143 static int pty_chr_read_poll(void *opaque
)
145 Chardev
*chr
= CHARDEV(opaque
);
146 PtyChardev
*s
= PTY_CHARDEV(opaque
);
148 s
->read_bytes
= qemu_chr_be_can_write(chr
);
149 return s
->read_bytes
;
152 static gboolean
pty_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
154 Chardev
*chr
= CHARDEV(opaque
);
155 PtyChardev
*s
= PTY_CHARDEV(opaque
);
157 uint8_t buf
[CHR_READ_BUF_LEN
];
161 if (len
> s
->read_bytes
) {
167 ret
= qio_channel_read(s
->ioc
, (char *)buf
, len
, NULL
);
169 pty_chr_state(chr
, 0);
172 pty_chr_state(chr
, 1);
173 qemu_chr_be_write(chr
, buf
, ret
);
178 static void pty_chr_state(Chardev
*chr
, int connected
)
180 PtyChardev
*s
= PTY_CHARDEV(chr
);
183 remove_fd_in_watch(chr
);
185 /* (re-)connect poll interval for idle guests: once per second.
186 * We check more frequently in case the guests sends data to
187 * the virtual device linked to our pty. */
188 pty_chr_rearm_timer(chr
, 1000);
190 pty_chr_timer_cancel(s
);
193 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
196 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
204 static void char_pty_finalize(Object
*obj
)
206 Chardev
*chr
= CHARDEV(obj
);
207 PtyChardev
*s
= PTY_CHARDEV(obj
);
215 pty_chr_state(chr
, 0);
216 object_unref(OBJECT(s
->ioc
));
217 pty_chr_timer_cancel(s
);
218 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
221 #if defined HAVE_PTY_H
223 #elif defined CONFIG_BSD
224 # include <termios.h>
225 # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
226 # include <libutil.h>
230 #elif defined CONFIG_SOLARIS
231 # include <termios.h>
232 # include <stropts.h>
234 # include <termios.h>
239 #if !defined(HAVE_OPENPTY)
240 /* Once illumos has openpty(), this is going to be removed. */
241 static int openpty(int *amaster
, int *aslave
, char *name
,
242 struct termios
*termp
, struct winsize
*winp
)
245 int mfd
= -1, sfd
= -1;
247 *amaster
= *aslave
= -1;
249 mfd
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
);
254 if (grantpt(mfd
) == -1 || unlockpt(mfd
) == -1) {
258 if ((slave
= ptsname(mfd
)) == NULL
) {
262 if ((sfd
= open(slave
, O_RDONLY
| O_NOCTTY
)) == -1) {
266 if (ioctl(sfd
, I_PUSH
, "ptem") == -1 ||
267 (termp
!= NULL
&& tcgetattr(sfd
, termp
) < 0)) {
275 ioctl(sfd
, TIOCSWINSZ
, winp
);
289 static void cfmakeraw (struct termios
*termios_p
)
291 termios_p
->c_iflag
&=
292 ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
);
293 termios_p
->c_oflag
&= ~OPOST
;
294 termios_p
->c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
295 termios_p
->c_cflag
&= ~(CSIZE
| PARENB
);
296 termios_p
->c_cflag
|= CS8
;
298 termios_p
->c_cc
[VMIN
] = 0;
299 termios_p
->c_cc
[VTIME
] = 0;
303 /* like openpty() but also makes it raw; return master fd */
304 static int qemu_openpty_raw(int *aslave
, char *pty_name
)
308 #if defined(__OpenBSD__) || defined(__DragonFly__)
309 char pty_buf
[PATH_MAX
];
310 #define q_ptsname(x) pty_buf
312 char *pty_buf
= NULL
;
313 #define q_ptsname(x) ptsname(x)
316 if (openpty(&amaster
, aslave
, pty_buf
, NULL
, NULL
) < 0) {
320 /* Set raw attributes on the pty. */
321 tcgetattr(*aslave
, &tty
);
323 tcsetattr(*aslave
, TCSAFLUSH
, &tty
);
326 strcpy(pty_name
, q_ptsname(amaster
));
332 static void char_pty_open(Chardev
*chr
,
333 ChardevBackend
*backend
,
338 int master_fd
, slave_fd
;
339 char pty_name
[PATH_MAX
];
341 char *path
= backend
->u
.pty
.data
->path
;
343 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
345 error_setg_errno(errp
, errno
, "Failed to create PTY");
350 if (!g_unix_set_fd_nonblocking(master_fd
, true, NULL
)) {
351 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
355 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
356 qemu_printf("char device redirected to %s (label %s)\n",
357 pty_name
, chr
->label
);
359 s
= PTY_CHARDEV(chr
);
360 s
->ioc
= QIO_CHANNEL(qio_channel_file_new_fd(master_fd
));
361 name
= g_strdup_printf("chardev-pty-%s", chr
->label
);
362 qio_channel_set_name(s
->ioc
, name
);
367 /* create symbolic link */
369 int res
= symlink(pty_name
, path
);
372 error_setg_errno(errp
, errno
, "Failed to create PTY symlink");
374 s
->path
= g_strdup(path
);
379 static void char_pty_parse(QemuOpts
*opts
, ChardevBackend
*backend
,
382 const char *path
= qemu_opt_get(opts
, "path");
385 backend
->type
= CHARDEV_BACKEND_KIND_PTY
;
386 pty
= backend
->u
.pty
.data
= g_new0(ChardevPty
, 1);
387 qemu_chr_parse_common(opts
, qapi_ChardevPty_base(pty
));
388 pty
->path
= g_strdup(path
);
391 static void char_pty_class_init(ObjectClass
*oc
, void *data
)
393 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
395 cc
->parse
= char_pty_parse
;
396 cc
->open
= char_pty_open
;
397 cc
->chr_write
= char_pty_chr_write
;
398 cc
->chr_update_read_handler
= pty_chr_update_read_handler
;
399 cc
->chr_add_watch
= pty_chr_add_watch
;
402 static const TypeInfo char_pty_type_info
= {
403 .name
= TYPE_CHARDEV_PTY
,
404 .parent
= TYPE_CHARDEV
,
405 .instance_size
= sizeof(PtyChardev
),
406 .instance_finalize
= char_pty_finalize
,
407 .class_init
= char_pty_class_init
,
410 static void register_types(void)
412 type_register_static(&char_pty_type_info
);
415 type_init(register_types
);