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
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"
32 #include "chardev/char-io.h"
39 /* Protected by the Chardev chr_write_lock. */
45 #define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY)
47 static void pty_chr_update_read_handler_locked(Chardev
*chr
);
48 static void pty_chr_state(Chardev
*chr
, int connected
);
50 static void pty_chr_timer_cancel(PtyChardev
*s
)
53 g_source_destroy(s
->timer_src
);
54 g_source_unref(s
->timer_src
);
59 static void pty_chr_open_src_cancel(PtyChardev
*s
)
62 g_source_destroy(s
->open_source
);
63 g_source_unref(s
->open_source
);
64 s
->open_source
= NULL
;
68 static gboolean
pty_chr_timer(gpointer opaque
)
70 struct Chardev
*chr
= CHARDEV(opaque
);
71 PtyChardev
*s
= PTY_CHARDEV(opaque
);
73 qemu_mutex_lock(&chr
->chr_write_lock
);
74 pty_chr_timer_cancel(s
);
75 pty_chr_open_src_cancel(s
);
78 pty_chr_update_read_handler_locked(chr
);
80 qemu_mutex_unlock(&chr
->chr_write_lock
);
84 /* Called with chr_write_lock held. */
85 static void pty_chr_rearm_timer(Chardev
*chr
, int ms
)
87 PtyChardev
*s
= PTY_CHARDEV(chr
);
90 pty_chr_timer_cancel(s
);
91 name
= g_strdup_printf("pty-timer-%s", chr
->label
);
92 s
->timer_src
= qemu_chr_timeout_add_ms(chr
, ms
, pty_chr_timer
, chr
);
93 g_source_set_name(s
->timer_src
, name
);
97 /* Called with chr_write_lock held. */
98 static void pty_chr_update_read_handler_locked(Chardev
*chr
)
100 PtyChardev
*s
= PTY_CHARDEV(chr
);
103 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(s
->ioc
);
106 pfd
.events
= G_IO_OUT
;
109 rc
= g_poll(&pfd
, 1, 0);
110 } while (rc
== -1 && errno
== EINTR
);
113 if (pfd
.revents
& G_IO_HUP
) {
114 pty_chr_state(chr
, 0);
116 pty_chr_state(chr
, 1);
120 static void pty_chr_update_read_handler(Chardev
*chr
)
122 qemu_mutex_lock(&chr
->chr_write_lock
);
123 pty_chr_update_read_handler_locked(chr
);
124 qemu_mutex_unlock(&chr
->chr_write_lock
);
127 /* Called with chr_write_lock held. */
128 static int char_pty_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
130 PtyChardev
*s
= PTY_CHARDEV(chr
);
133 /* guest sends data, check for (re-)connect */
134 pty_chr_update_read_handler_locked(chr
);
139 return io_channel_send(s
->ioc
, buf
, len
);
142 static GSource
*pty_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
144 PtyChardev
*s
= PTY_CHARDEV(chr
);
148 return qio_channel_create_watch(s
->ioc
, cond
);
151 static int pty_chr_read_poll(void *opaque
)
153 Chardev
*chr
= CHARDEV(opaque
);
154 PtyChardev
*s
= PTY_CHARDEV(opaque
);
156 s
->read_bytes
= qemu_chr_be_can_write(chr
);
157 return s
->read_bytes
;
160 static gboolean
pty_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
162 Chardev
*chr
= CHARDEV(opaque
);
163 PtyChardev
*s
= PTY_CHARDEV(opaque
);
165 uint8_t buf
[CHR_READ_BUF_LEN
];
169 if (len
> s
->read_bytes
) {
175 ret
= qio_channel_read(s
->ioc
, (char *)buf
, len
, NULL
);
177 pty_chr_state(chr
, 0);
180 pty_chr_state(chr
, 1);
181 qemu_chr_be_write(chr
, buf
, ret
);
186 static gboolean
qemu_chr_be_generic_open_func(gpointer opaque
)
188 Chardev
*chr
= CHARDEV(opaque
);
189 PtyChardev
*s
= PTY_CHARDEV(opaque
);
191 s
->open_source
= NULL
;
192 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
196 /* Called with chr_write_lock held. */
197 static void pty_chr_state(Chardev
*chr
, int connected
)
199 PtyChardev
*s
= PTY_CHARDEV(chr
);
202 pty_chr_open_src_cancel(s
);
203 remove_fd_in_watch(chr
);
205 /* (re-)connect poll interval for idle guests: once per second.
206 * We check more frequently in case the guests sends data to
207 * the virtual device linked to our pty. */
208 pty_chr_rearm_timer(chr
, 1000);
210 pty_chr_timer_cancel(s
);
212 g_assert(s
->open_source
== NULL
);
213 s
->open_source
= g_idle_source_new();
215 g_source_set_callback(s
->open_source
,
216 qemu_chr_be_generic_open_func
,
218 g_source_attach(s
->open_source
, chr
->gcontext
);
221 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
229 static void char_pty_finalize(Object
*obj
)
231 Chardev
*chr
= CHARDEV(obj
);
232 PtyChardev
*s
= PTY_CHARDEV(obj
);
234 qemu_mutex_lock(&chr
->chr_write_lock
);
235 pty_chr_state(chr
, 0);
236 object_unref(OBJECT(s
->ioc
));
237 pty_chr_timer_cancel(s
);
238 qemu_mutex_unlock(&chr
->chr_write_lock
);
239 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
242 static void char_pty_open(Chardev
*chr
,
243 ChardevBackend
*backend
,
248 int master_fd
, slave_fd
;
249 char pty_name
[PATH_MAX
];
252 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
254 error_setg_errno(errp
, errno
, "Failed to create PTY");
259 qemu_set_nonblock(master_fd
);
261 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
262 error_printf("char device redirected to %s (label %s)\n",
263 pty_name
, chr
->label
);
265 s
= PTY_CHARDEV(chr
);
266 s
->ioc
= QIO_CHANNEL(qio_channel_file_new_fd(master_fd
));
267 name
= g_strdup_printf("chardev-pty-%s", chr
->label
);
268 qio_channel_set_name(QIO_CHANNEL(s
->ioc
), name
);
274 static void char_pty_class_init(ObjectClass
*oc
, void *data
)
276 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
278 cc
->open
= char_pty_open
;
279 cc
->chr_write
= char_pty_chr_write
;
280 cc
->chr_update_read_handler
= pty_chr_update_read_handler
;
281 cc
->chr_add_watch
= pty_chr_add_watch
;
284 static const TypeInfo char_pty_type_info
= {
285 .name
= TYPE_CHARDEV_PTY
,
286 .parent
= TYPE_CHARDEV
,
287 .instance_size
= sizeof(PtyChardev
),
288 .instance_finalize
= char_pty_finalize
,
289 .class_init
= char_pty_class_init
,
292 static void register_types(void)
294 type_register_static(&char_pty_type_info
);
297 type_init(register_types
);