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 "qemu-common.h"
27 #include "qapi/error.h"
28 #include "chardev/char.h"
29 #include "io/channel-file.h"
30 #include "qemu/sockets.h"
31 #include "qemu/error-report.h"
32 #include "qemu/module.h"
33 #include "qemu/qemu-print.h"
35 #include "chardev/char-io.h"
36 #include "qom/object.h"
46 typedef struct PtyChardev PtyChardev
;
48 DECLARE_INSTANCE_CHECKER(PtyChardev
, PTY_CHARDEV
,
51 static void pty_chr_state(Chardev
*chr
, int connected
);
53 static void pty_chr_timer_cancel(PtyChardev
*s
)
56 g_source_destroy(s
->timer_src
);
57 g_source_unref(s
->timer_src
);
62 static gboolean
pty_chr_timer(gpointer opaque
)
64 struct Chardev
*chr
= CHARDEV(opaque
);
65 PtyChardev
*s
= PTY_CHARDEV(opaque
);
67 pty_chr_timer_cancel(s
);
70 qemu_chr_be_update_read_handlers(chr
, chr
->gcontext
);
75 static void pty_chr_rearm_timer(Chardev
*chr
, int ms
)
77 PtyChardev
*s
= PTY_CHARDEV(chr
);
80 pty_chr_timer_cancel(s
);
81 name
= g_strdup_printf("pty-timer-%s", chr
->label
);
82 s
->timer_src
= qemu_chr_timeout_add_ms(chr
, ms
, pty_chr_timer
, chr
);
83 g_source_set_name(s
->timer_src
, name
);
87 static void pty_chr_update_read_handler(Chardev
*chr
)
89 PtyChardev
*s
= PTY_CHARDEV(chr
);
92 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(s
->ioc
);
95 pfd
.events
= G_IO_OUT
;
98 rc
= g_poll(&pfd
, 1, 0);
99 } while (rc
== -1 && errno
== EINTR
);
102 if (pfd
.revents
& G_IO_HUP
) {
103 pty_chr_state(chr
, 0);
105 pty_chr_state(chr
, 1);
109 static int char_pty_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
111 PtyChardev
*s
= PTY_CHARDEV(chr
);
116 return io_channel_send(s
->ioc
, buf
, len
);
119 static GSource
*pty_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
121 PtyChardev
*s
= PTY_CHARDEV(chr
);
125 return qio_channel_create_watch(s
->ioc
, cond
);
128 static int pty_chr_read_poll(void *opaque
)
130 Chardev
*chr
= CHARDEV(opaque
);
131 PtyChardev
*s
= PTY_CHARDEV(opaque
);
133 s
->read_bytes
= qemu_chr_be_can_write(chr
);
134 return s
->read_bytes
;
137 static gboolean
pty_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
139 Chardev
*chr
= CHARDEV(opaque
);
140 PtyChardev
*s
= PTY_CHARDEV(opaque
);
142 uint8_t buf
[CHR_READ_BUF_LEN
];
146 if (len
> s
->read_bytes
) {
152 ret
= qio_channel_read(s
->ioc
, (char *)buf
, len
, NULL
);
154 pty_chr_state(chr
, 0);
157 pty_chr_state(chr
, 1);
158 qemu_chr_be_write(chr
, buf
, ret
);
163 static void pty_chr_state(Chardev
*chr
, int connected
)
165 PtyChardev
*s
= PTY_CHARDEV(chr
);
168 remove_fd_in_watch(chr
);
170 /* (re-)connect poll interval for idle guests: once per second.
171 * We check more frequently in case the guests sends data to
172 * the virtual device linked to our pty. */
173 pty_chr_rearm_timer(chr
, 1000);
175 pty_chr_timer_cancel(s
);
178 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
181 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
189 static void char_pty_finalize(Object
*obj
)
191 Chardev
*chr
= CHARDEV(obj
);
192 PtyChardev
*s
= PTY_CHARDEV(obj
);
194 pty_chr_state(chr
, 0);
195 object_unref(OBJECT(s
->ioc
));
196 pty_chr_timer_cancel(s
);
197 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
200 static void char_pty_open(Chardev
*chr
,
201 ChardevBackend
*backend
,
206 int master_fd
, slave_fd
;
207 char pty_name
[PATH_MAX
];
210 master_fd
= qemu_openpty_raw(&slave_fd
, pty_name
);
212 error_setg_errno(errp
, errno
, "Failed to create PTY");
217 qemu_set_nonblock(master_fd
);
219 chr
->filename
= g_strdup_printf("pty:%s", pty_name
);
220 qemu_printf("char device redirected to %s (label %s)\n",
221 pty_name
, chr
->label
);
223 s
= PTY_CHARDEV(chr
);
224 s
->ioc
= QIO_CHANNEL(qio_channel_file_new_fd(master_fd
));
225 name
= g_strdup_printf("chardev-pty-%s", chr
->label
);
226 qio_channel_set_name(QIO_CHANNEL(s
->ioc
), name
);
232 static void char_pty_class_init(ObjectClass
*oc
, void *data
)
234 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
236 cc
->open
= char_pty_open
;
237 cc
->chr_write
= char_pty_chr_write
;
238 cc
->chr_update_read_handler
= pty_chr_update_read_handler
;
239 cc
->chr_add_watch
= pty_chr_add_watch
;
242 static const TypeInfo char_pty_type_info
= {
243 .name
= TYPE_CHARDEV_PTY
,
244 .parent
= TYPE_CHARDEV
,
245 .instance_size
= sizeof(PtyChardev
),
246 .instance_finalize
= char_pty_finalize
,
247 .class_init
= char_pty_class_init
,
250 static void register_types(void)
252 type_register_static(&char_pty_type_info
);
255 type_init(register_types
);