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