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