]>
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 | */ | |
24 | #include "qemu/osdep.h" | |
25 | #include "qapi/error.h" | |
26 | #include "qemu-common.h" | |
8228e353 | 27 | #include "chardev/char.h" |
c3b7d620 MAL |
28 | #include "io/channel-file.h" |
29 | #include "qemu/sockets.h" | |
30 | #include "qemu/error-report.h" | |
31 | ||
8228e353 | 32 | #include "chardev/char-io.h" |
c3b7d620 | 33 | |
c3b7d620 MAL |
34 | typedef struct { |
35 | Chardev parent; | |
36 | QIOChannel *ioc; | |
37 | int read_bytes; | |
38 | ||
39 | /* Protected by the Chardev chr_write_lock. */ | |
40 | int connected; | |
2c716ba1 | 41 | GSource *timer_src; |
938eb9e9 | 42 | GSource *open_source; |
c3b7d620 MAL |
43 | } PtyChardev; |
44 | ||
45 | #define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY) | |
46 | ||
47 | static void pty_chr_update_read_handler_locked(Chardev *chr); | |
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 | ||
59 | static void pty_chr_open_src_cancel(PtyChardev *s) | |
60 | { | |
61 | if (s->open_source) { | |
62 | g_source_destroy(s->open_source); | |
63 | g_source_unref(s->open_source); | |
64 | s->open_source = NULL; | |
65 | } | |
66 | } | |
67 | ||
c3b7d620 MAL |
68 | static gboolean pty_chr_timer(gpointer opaque) |
69 | { | |
70 | struct Chardev *chr = CHARDEV(opaque); | |
71 | PtyChardev *s = PTY_CHARDEV(opaque); | |
72 | ||
73 | qemu_mutex_lock(&chr->chr_write_lock); | |
4e4b5734 PX |
74 | pty_chr_timer_cancel(s); |
75 | pty_chr_open_src_cancel(s); | |
c3b7d620 MAL |
76 | if (!s->connected) { |
77 | /* Next poll ... */ | |
78 | pty_chr_update_read_handler_locked(chr); | |
79 | } | |
80 | qemu_mutex_unlock(&chr->chr_write_lock); | |
81 | return FALSE; | |
82 | } | |
83 | ||
84 | /* Called with chr_write_lock held. */ | |
85 | static void pty_chr_rearm_timer(Chardev *chr, int ms) | |
86 | { | |
87 | PtyChardev *s = PTY_CHARDEV(chr); | |
88 | char *name; | |
89 | ||
2c716ba1 PX |
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); | |
c3b7d620 MAL |
94 | g_free(name); |
95 | } | |
96 | ||
97 | /* Called with chr_write_lock held. */ | |
98 | static void pty_chr_update_read_handler_locked(Chardev *chr) | |
99 | { | |
100 | PtyChardev *s = PTY_CHARDEV(chr); | |
101 | GPollFD pfd; | |
102 | int rc; | |
103 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc); | |
104 | ||
105 | pfd.fd = fioc->fd; | |
106 | pfd.events = G_IO_OUT; | |
107 | pfd.revents = 0; | |
108 | do { | |
109 | rc = g_poll(&pfd, 1, 0); | |
110 | } while (rc == -1 && errno == EINTR); | |
111 | assert(rc >= 0); | |
112 | ||
113 | if (pfd.revents & G_IO_HUP) { | |
114 | pty_chr_state(chr, 0); | |
115 | } else { | |
116 | pty_chr_state(chr, 1); | |
117 | } | |
118 | } | |
119 | ||
bb86d05f | 120 | static void pty_chr_update_read_handler(Chardev *chr) |
c3b7d620 MAL |
121 | { |
122 | qemu_mutex_lock(&chr->chr_write_lock); | |
123 | pty_chr_update_read_handler_locked(chr); | |
124 | qemu_mutex_unlock(&chr->chr_write_lock); | |
125 | } | |
126 | ||
127 | /* Called with chr_write_lock held. */ | |
128 | static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len) | |
129 | { | |
130 | PtyChardev *s = PTY_CHARDEV(chr); | |
131 | ||
132 | if (!s->connected) { | |
f8278c7d | 133 | return len; |
c3b7d620 MAL |
134 | } |
135 | return io_channel_send(s->ioc, buf, len); | |
136 | } | |
137 | ||
138 | static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond) | |
139 | { | |
140 | PtyChardev *s = PTY_CHARDEV(chr); | |
141 | if (!s->connected) { | |
142 | return NULL; | |
143 | } | |
144 | return qio_channel_create_watch(s->ioc, cond); | |
145 | } | |
146 | ||
147 | static int pty_chr_read_poll(void *opaque) | |
148 | { | |
149 | Chardev *chr = CHARDEV(opaque); | |
150 | PtyChardev *s = PTY_CHARDEV(opaque); | |
151 | ||
152 | s->read_bytes = qemu_chr_be_can_write(chr); | |
153 | return s->read_bytes; | |
154 | } | |
155 | ||
156 | static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) | |
157 | { | |
158 | Chardev *chr = CHARDEV(opaque); | |
159 | PtyChardev *s = PTY_CHARDEV(opaque); | |
160 | gsize len; | |
161 | uint8_t buf[CHR_READ_BUF_LEN]; | |
162 | ssize_t ret; | |
163 | ||
164 | len = sizeof(buf); | |
165 | if (len > s->read_bytes) { | |
166 | len = s->read_bytes; | |
167 | } | |
168 | if (len == 0) { | |
169 | return TRUE; | |
170 | } | |
171 | ret = qio_channel_read(s->ioc, (char *)buf, len, NULL); | |
172 | if (ret <= 0) { | |
173 | pty_chr_state(chr, 0); | |
174 | return FALSE; | |
175 | } else { | |
176 | pty_chr_state(chr, 1); | |
177 | qemu_chr_be_write(chr, buf, ret); | |
178 | } | |
179 | return TRUE; | |
180 | } | |
181 | ||
182 | static gboolean qemu_chr_be_generic_open_func(gpointer opaque) | |
183 | { | |
184 | Chardev *chr = CHARDEV(opaque); | |
185 | PtyChardev *s = PTY_CHARDEV(opaque); | |
186 | ||
938eb9e9 | 187 | s->open_source = NULL; |
63618135 | 188 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
c3b7d620 MAL |
189 | return FALSE; |
190 | } | |
191 | ||
192 | /* Called with chr_write_lock held. */ | |
193 | static void pty_chr_state(Chardev *chr, int connected) | |
194 | { | |
195 | PtyChardev *s = PTY_CHARDEV(chr); | |
196 | ||
197 | if (!connected) { | |
4e4b5734 | 198 | pty_chr_open_src_cancel(s); |
b19456dd | 199 | remove_fd_in_watch(chr); |
c3b7d620 MAL |
200 | s->connected = 0; |
201 | /* (re-)connect poll interval for idle guests: once per second. | |
202 | * We check more frequently in case the guests sends data to | |
203 | * the virtual device linked to our pty. */ | |
204 | pty_chr_rearm_timer(chr, 1000); | |
205 | } else { | |
2c716ba1 | 206 | pty_chr_timer_cancel(s); |
c3b7d620 | 207 | if (!s->connected) { |
938eb9e9 PX |
208 | g_assert(s->open_source == NULL); |
209 | s->open_source = g_idle_source_new(); | |
c3b7d620 | 210 | s->connected = 1; |
938eb9e9 PX |
211 | g_source_set_callback(s->open_source, |
212 | qemu_chr_be_generic_open_func, | |
213 | chr, NULL); | |
214 | g_source_attach(s->open_source, chr->gcontext); | |
c3b7d620 | 215 | } |
b19456dd HZ |
216 | if (!chr->gsource) { |
217 | chr->gsource = io_add_watch_poll(chr, s->ioc, | |
c3b7d620 MAL |
218 | pty_chr_read_poll, |
219 | pty_chr_read, | |
6bbb6c06 | 220 | chr, chr->gcontext); |
c3b7d620 MAL |
221 | } |
222 | } | |
223 | } | |
224 | ||
225 | static void char_pty_finalize(Object *obj) | |
226 | { | |
227 | Chardev *chr = CHARDEV(obj); | |
228 | PtyChardev *s = PTY_CHARDEV(obj); | |
229 | ||
230 | qemu_mutex_lock(&chr->chr_write_lock); | |
231 | pty_chr_state(chr, 0); | |
232 | object_unref(OBJECT(s->ioc)); | |
2c716ba1 | 233 | pty_chr_timer_cancel(s); |
c3b7d620 MAL |
234 | qemu_mutex_unlock(&chr->chr_write_lock); |
235 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); | |
236 | } | |
237 | ||
238 | static void char_pty_open(Chardev *chr, | |
239 | ChardevBackend *backend, | |
240 | bool *be_opened, | |
241 | Error **errp) | |
242 | { | |
243 | PtyChardev *s; | |
244 | int master_fd, slave_fd; | |
245 | char pty_name[PATH_MAX]; | |
246 | char *name; | |
247 | ||
248 | master_fd = qemu_openpty_raw(&slave_fd, pty_name); | |
249 | if (master_fd < 0) { | |
250 | error_setg_errno(errp, errno, "Failed to create PTY"); | |
251 | return; | |
252 | } | |
253 | ||
254 | close(slave_fd); | |
255 | qemu_set_nonblock(master_fd); | |
256 | ||
257 | chr->filename = g_strdup_printf("pty:%s", pty_name); | |
b1b769b0 | 258 | error_printf("char device redirected to %s (label %s)\n", |
c3b7d620 MAL |
259 | pty_name, chr->label); |
260 | ||
261 | s = PTY_CHARDEV(chr); | |
262 | s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd)); | |
263 | name = g_strdup_printf("chardev-pty-%s", chr->label); | |
264 | qio_channel_set_name(QIO_CHANNEL(s->ioc), name); | |
265 | g_free(name); | |
2c716ba1 | 266 | s->timer_src = NULL; |
c3b7d620 MAL |
267 | *be_opened = false; |
268 | } | |
269 | ||
270 | static void char_pty_class_init(ObjectClass *oc, void *data) | |
271 | { | |
272 | ChardevClass *cc = CHARDEV_CLASS(oc); | |
273 | ||
274 | cc->open = char_pty_open; | |
275 | cc->chr_write = char_pty_chr_write; | |
276 | cc->chr_update_read_handler = pty_chr_update_read_handler; | |
277 | cc->chr_add_watch = pty_chr_add_watch; | |
278 | } | |
279 | ||
280 | static const TypeInfo char_pty_type_info = { | |
281 | .name = TYPE_CHARDEV_PTY, | |
282 | .parent = TYPE_CHARDEV, | |
283 | .instance_size = sizeof(PtyChardev), | |
284 | .instance_finalize = char_pty_finalize, | |
285 | .class_init = char_pty_class_init, | |
286 | }; | |
287 | ||
288 | static void register_types(void) | |
289 | { | |
290 | type_register_static(&char_pty_type_info); | |
291 | } | |
292 | ||
293 | type_init(register_types); |