]>
Commit | Line | Data |
---|---|---|
c3b7d620 MAL |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
0b8fa32f | 24 | |
c3b7d620 | 25 | #include "qemu/osdep.h" |
a8d25326 | 26 | #include "qemu-common.h" |
c3b7d620 | 27 | #include "qapi/error.h" |
8228e353 | 28 | #include "chardev/char.h" |
c3b7d620 MAL |
29 | #include "io/channel-file.h" |
30 | #include "qemu/sockets.h" | |
31 | #include "qemu/error-report.h" | |
0b8fa32f | 32 | #include "qemu/module.h" |
6ade45f2 | 33 | #include "qemu/qemu-print.h" |
c3b7d620 | 34 | |
8228e353 | 35 | #include "chardev/char-io.h" |
c3b7d620 | 36 | |
c3b7d620 MAL |
37 | typedef struct { |
38 | Chardev parent; | |
39 | QIOChannel *ioc; | |
40 | int read_bytes; | |
41 | ||
c3b7d620 | 42 | int connected; |
2c716ba1 | 43 | GSource *timer_src; |
c3b7d620 MAL |
44 | } PtyChardev; |
45 | ||
46 | #define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY) | |
47 | ||
c3b7d620 MAL |
48 | static void pty_chr_state(Chardev *chr, int connected); |
49 | ||
4e4b5734 PX |
50 | static void pty_chr_timer_cancel(PtyChardev *s) |
51 | { | |
52 | if (s->timer_src) { | |
53 | g_source_destroy(s->timer_src); | |
54 | g_source_unref(s->timer_src); | |
55 | s->timer_src = NULL; | |
56 | } | |
57 | } | |
58 | ||
c3b7d620 MAL |
59 | static gboolean pty_chr_timer(gpointer opaque) |
60 | { | |
61 | struct Chardev *chr = CHARDEV(opaque); | |
62 | PtyChardev *s = PTY_CHARDEV(opaque); | |
63 | ||
4e4b5734 | 64 | pty_chr_timer_cancel(s); |
c3b7d620 MAL |
65 | if (!s->connected) { |
66 | /* Next poll ... */ | |
f7ea2038 | 67 | qemu_chr_be_update_read_handlers(chr, chr->gcontext); |
c3b7d620 | 68 | } |
c3b7d620 MAL |
69 | return FALSE; |
70 | } | |
71 | ||
c3b7d620 MAL |
72 | static void pty_chr_rearm_timer(Chardev *chr, int ms) |
73 | { | |
74 | PtyChardev *s = PTY_CHARDEV(chr); | |
75 | char *name; | |
76 | ||
2c716ba1 PX |
77 | pty_chr_timer_cancel(s); |
78 | name = g_strdup_printf("pty-timer-%s", chr->label); | |
79 | s->timer_src = qemu_chr_timeout_add_ms(chr, ms, pty_chr_timer, chr); | |
80 | g_source_set_name(s->timer_src, name); | |
c3b7d620 MAL |
81 | g_free(name); |
82 | } | |
83 | ||
f7ea2038 | 84 | static void pty_chr_update_read_handler(Chardev *chr) |
c3b7d620 MAL |
85 | { |
86 | PtyChardev *s = PTY_CHARDEV(chr); | |
87 | GPollFD pfd; | |
88 | int rc; | |
89 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc); | |
90 | ||
91 | pfd.fd = fioc->fd; | |
92 | pfd.events = G_IO_OUT; | |
93 | pfd.revents = 0; | |
94 | do { | |
95 | rc = g_poll(&pfd, 1, 0); | |
96 | } while (rc == -1 && errno == EINTR); | |
97 | assert(rc >= 0); | |
98 | ||
99 | if (pfd.revents & G_IO_HUP) { | |
100 | pty_chr_state(chr, 0); | |
101 | } else { | |
102 | pty_chr_state(chr, 1); | |
103 | } | |
104 | } | |
105 | ||
c3b7d620 MAL |
106 | static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len) |
107 | { | |
108 | PtyChardev *s = PTY_CHARDEV(chr); | |
109 | ||
110 | if (!s->connected) { | |
f8278c7d | 111 | return len; |
c3b7d620 MAL |
112 | } |
113 | return io_channel_send(s->ioc, buf, len); | |
114 | } | |
115 | ||
116 | static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond) | |
117 | { | |
118 | PtyChardev *s = PTY_CHARDEV(chr); | |
119 | if (!s->connected) { | |
120 | return NULL; | |
121 | } | |
122 | return qio_channel_create_watch(s->ioc, cond); | |
123 | } | |
124 | ||
125 | static int pty_chr_read_poll(void *opaque) | |
126 | { | |
127 | Chardev *chr = CHARDEV(opaque); | |
128 | PtyChardev *s = PTY_CHARDEV(opaque); | |
129 | ||
130 | s->read_bytes = qemu_chr_be_can_write(chr); | |
131 | return s->read_bytes; | |
132 | } | |
133 | ||
134 | static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) | |
135 | { | |
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; | |
141 | ||
142 | len = sizeof(buf); | |
143 | if (len > s->read_bytes) { | |
144 | len = s->read_bytes; | |
145 | } | |
146 | if (len == 0) { | |
147 | return TRUE; | |
148 | } | |
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); | |
156 | } | |
157 | return TRUE; | |
158 | } | |
159 | ||
c3b7d620 MAL |
160 | static void pty_chr_state(Chardev *chr, int connected) |
161 | { | |
162 | PtyChardev *s = PTY_CHARDEV(chr); | |
163 | ||
164 | if (!connected) { | |
b19456dd | 165 | remove_fd_in_watch(chr); |
c3b7d620 MAL |
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 { | |
2c716ba1 | 172 | pty_chr_timer_cancel(s); |
c3b7d620 | 173 | if (!s->connected) { |
c3b7d620 | 174 | s->connected = 1; |
f7ea2038 | 175 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
c3b7d620 | 176 | } |
b19456dd HZ |
177 | if (!chr->gsource) { |
178 | chr->gsource = io_add_watch_poll(chr, s->ioc, | |
c3b7d620 MAL |
179 | pty_chr_read_poll, |
180 | pty_chr_read, | |
6bbb6c06 | 181 | chr, chr->gcontext); |
c3b7d620 MAL |
182 | } |
183 | } | |
184 | } | |
185 | ||
186 | static void char_pty_finalize(Object *obj) | |
187 | { | |
188 | Chardev *chr = CHARDEV(obj); | |
189 | PtyChardev *s = PTY_CHARDEV(obj); | |
190 | ||
c3b7d620 MAL |
191 | pty_chr_state(chr, 0); |
192 | object_unref(OBJECT(s->ioc)); | |
2c716ba1 | 193 | pty_chr_timer_cancel(s); |
c3b7d620 MAL |
194 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
195 | } | |
196 | ||
197 | static void char_pty_open(Chardev *chr, | |
198 | ChardevBackend *backend, | |
199 | bool *be_opened, | |
200 | Error **errp) | |
201 | { | |
202 | PtyChardev *s; | |
203 | int master_fd, slave_fd; | |
204 | char pty_name[PATH_MAX]; | |
205 | char *name; | |
206 | ||
207 | master_fd = qemu_openpty_raw(&slave_fd, pty_name); | |
208 | if (master_fd < 0) { | |
209 | error_setg_errno(errp, errno, "Failed to create PTY"); | |
210 | return; | |
211 | } | |
212 | ||
213 | close(slave_fd); | |
214 | qemu_set_nonblock(master_fd); | |
215 | ||
216 | chr->filename = g_strdup_printf("pty:%s", pty_name); | |
6ade45f2 MA |
217 | qemu_printf("char device redirected to %s (label %s)\n", |
218 | pty_name, chr->label); | |
c3b7d620 MAL |
219 | |
220 | s = PTY_CHARDEV(chr); | |
221 | s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd)); | |
222 | name = g_strdup_printf("chardev-pty-%s", chr->label); | |
223 | qio_channel_set_name(QIO_CHANNEL(s->ioc), name); | |
224 | g_free(name); | |
2c716ba1 | 225 | s->timer_src = NULL; |
c3b7d620 MAL |
226 | *be_opened = false; |
227 | } | |
228 | ||
229 | static void char_pty_class_init(ObjectClass *oc, void *data) | |
230 | { | |
231 | ChardevClass *cc = CHARDEV_CLASS(oc); | |
232 | ||
233 | cc->open = char_pty_open; | |
234 | cc->chr_write = char_pty_chr_write; | |
235 | cc->chr_update_read_handler = pty_chr_update_read_handler; | |
236 | cc->chr_add_watch = pty_chr_add_watch; | |
237 | } | |
238 | ||
239 | static const TypeInfo char_pty_type_info = { | |
240 | .name = TYPE_CHARDEV_PTY, | |
241 | .parent = TYPE_CHARDEV, | |
242 | .instance_size = sizeof(PtyChardev), | |
243 | .instance_finalize = char_pty_finalize, | |
244 | .class_init = char_pty_class_init, | |
245 | }; | |
246 | ||
247 | static void register_types(void) | |
248 | { | |
249 | type_register_static(&char_pty_type_info); | |
250 | } | |
251 | ||
252 | type_init(register_types); |