xref: /openbmc/qemu/chardev/char-fe.c (revision 4a18751c)
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 "qemu/error-report.h"
26 #include "qapi/error.h"
27 #include "sysemu/replay.h"
28 
29 #include "chardev/char-fe.h"
30 #include "chardev/char-io.h"
31 #include "chardev-internal.h"
32 
qemu_chr_fe_write(CharBackend * be,const uint8_t * buf,int len)33 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)
34 {
35     Chardev *s = be->chr;
36 
37     if (!s) {
38         return 0;
39     }
40 
41     return qemu_chr_write(s, buf, len, false);
42 }
43 
qemu_chr_fe_write_all(CharBackend * be,const uint8_t * buf,int len)44 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len)
45 {
46     Chardev *s = be->chr;
47 
48     if (!s) {
49         return 0;
50     }
51 
52     return qemu_chr_write(s, buf, len, true);
53 }
54 
qemu_chr_fe_read_all(CharBackend * be,uint8_t * buf,int len)55 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)
56 {
57     Chardev *s = be->chr;
58     int offset = 0;
59     int res;
60 
61     if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) {
62         return 0;
63     }
64 
65     if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
66         return replay_char_read_all_load(buf);
67     }
68 
69     while (offset < len) {
70     retry:
71         res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset,
72                                                   len - offset);
73         if (res == -1 && errno == EAGAIN) {
74             g_usleep(100);
75             goto retry;
76         }
77 
78         if (res == 0) {
79             break;
80         }
81 
82         if (res < 0) {
83             if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
84                 replay_char_read_all_save_error(res);
85             }
86             return res;
87         }
88 
89         offset += res;
90     }
91 
92     if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
93         replay_char_read_all_save_buf(buf, offset);
94     }
95     return offset;
96 }
97 
qemu_chr_fe_ioctl(CharBackend * be,int cmd,void * arg)98 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
99 {
100     Chardev *s = be->chr;
101     int res;
102 
103     if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) {
104         res = -ENOTSUP;
105     } else {
106         res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg);
107     }
108 
109     return res;
110 }
111 
qemu_chr_fe_get_msgfd(CharBackend * be)112 int qemu_chr_fe_get_msgfd(CharBackend *be)
113 {
114     Chardev *s = be->chr;
115     int fd;
116     int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1;
117     if (s && qemu_chr_replay(s)) {
118         error_report("Replay: get msgfd is not supported "
119                      "for serial devices yet");
120         exit(1);
121     }
122     return res;
123 }
124 
qemu_chr_fe_get_msgfds(CharBackend * be,int * fds,int len)125 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
126 {
127     Chardev *s = be->chr;
128 
129     if (!s) {
130         return -1;
131     }
132 
133     return CHARDEV_GET_CLASS(s)->get_msgfds ?
134         CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1;
135 }
136 
qemu_chr_fe_set_msgfds(CharBackend * be,int * fds,int num)137 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
138 {
139     Chardev *s = be->chr;
140 
141     if (!s) {
142         return -1;
143     }
144 
145     return CHARDEV_GET_CLASS(s)->set_msgfds ?
146         CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1;
147 }
148 
qemu_chr_fe_accept_input(CharBackend * be)149 void qemu_chr_fe_accept_input(CharBackend *be)
150 {
151     Chardev *s = be->chr;
152 
153     if (!s) {
154         return;
155     }
156 
157     if (CHARDEV_GET_CLASS(s)->chr_accept_input) {
158         CHARDEV_GET_CLASS(s)->chr_accept_input(s);
159     }
160     qemu_notify_event();
161 }
162 
qemu_chr_fe_printf(CharBackend * be,const char * fmt,...)163 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
164 {
165     char buf[CHR_READ_BUF_LEN];
166     va_list ap;
167     va_start(ap, fmt);
168     vsnprintf(buf, sizeof(buf), fmt, ap);
169     /* XXX this blocks entire thread. Rewrite to use
170      * qemu_chr_fe_write and background I/O callbacks */
171     qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf));
172     va_end(ap);
173 }
174 
qemu_chr_fe_get_driver(CharBackend * be)175 Chardev *qemu_chr_fe_get_driver(CharBackend *be)
176 {
177     /* this is unsafe for the users that support chardev hotswap */
178     assert(be->chr_be_change == NULL);
179     return be->chr;
180 }
181 
qemu_chr_fe_backend_connected(CharBackend * be)182 bool qemu_chr_fe_backend_connected(CharBackend *be)
183 {
184     return !!be->chr;
185 }
186 
qemu_chr_fe_backend_open(CharBackend * be)187 bool qemu_chr_fe_backend_open(CharBackend *be)
188 {
189     return be->chr && be->chr->be_open;
190 }
191 
qemu_chr_fe_init(CharBackend * b,Chardev * s,Error ** errp)192 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
193 {
194     int tag = 0;
195 
196     if (s) {
197         if (CHARDEV_IS_MUX(s)) {
198             MuxChardev *d = MUX_CHARDEV(s);
199 
200             if (d->mux_cnt >= MAX_MUX) {
201                 error_setg(errp,
202                            "too many uses of multiplexed chardev '%s'"
203                            " (maximum is " stringify(MAX_MUX) ")",
204                            s->label);
205                 return false;
206             }
207 
208             d->backends[d->mux_cnt] = b;
209             tag = d->mux_cnt++;
210         } else if (s->be) {
211             error_setg(errp, "chardev '%s' is already in use", s->label);
212             return false;
213         } else {
214             s->be = b;
215         }
216     }
217 
218     b->fe_is_open = false;
219     b->tag = tag;
220     b->chr = s;
221     return true;
222 }
223 
qemu_chr_fe_deinit(CharBackend * b,bool del)224 void qemu_chr_fe_deinit(CharBackend *b, bool del)
225 {
226     assert(b);
227 
228     if (b->chr) {
229         qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, NULL, true);
230         if (b->chr->be == b) {
231             b->chr->be = NULL;
232         }
233         if (CHARDEV_IS_MUX(b->chr)) {
234             MuxChardev *d = MUX_CHARDEV(b->chr);
235             d->backends[b->tag] = NULL;
236         }
237         if (del) {
238             Object *obj = OBJECT(b->chr);
239             if (obj->parent) {
240                 object_unparent(obj);
241             } else {
242                 object_unref(obj);
243             }
244         }
245         b->chr = NULL;
246     }
247 }
248 
qemu_chr_fe_set_handlers_full(CharBackend * b,IOCanReadHandler * fd_can_read,IOReadHandler * fd_read,IOEventHandler * fd_event,BackendChangeHandler * be_change,void * opaque,GMainContext * context,bool set_open,bool sync_state)249 void qemu_chr_fe_set_handlers_full(CharBackend *b,
250                                    IOCanReadHandler *fd_can_read,
251                                    IOReadHandler *fd_read,
252                                    IOEventHandler *fd_event,
253                                    BackendChangeHandler *be_change,
254                                    void *opaque,
255                                    GMainContext *context,
256                                    bool set_open,
257                                    bool sync_state)
258 {
259     Chardev *s;
260     bool fe_open;
261 
262     s = b->chr;
263     if (!s) {
264         return;
265     }
266 
267     if (!opaque && !fd_can_read && !fd_read && !fd_event) {
268         fe_open = false;
269         remove_fd_in_watch(s);
270     } else {
271         fe_open = true;
272     }
273     b->chr_can_read = fd_can_read;
274     b->chr_read = fd_read;
275     b->chr_event = fd_event;
276     b->chr_be_change = be_change;
277     b->opaque = opaque;
278 
279     qemu_chr_be_update_read_handlers(s, context);
280 
281     if (set_open) {
282         qemu_chr_fe_set_open(b, fe_open);
283     }
284 
285     if (fe_open) {
286         qemu_chr_fe_take_focus(b);
287         /* We're connecting to an already opened device, so let's make sure we
288            also get the open event */
289         if (sync_state && s->be_open) {
290             qemu_chr_be_event(s, CHR_EVENT_OPENED);
291         }
292     }
293 }
294 
qemu_chr_fe_set_handlers(CharBackend * b,IOCanReadHandler * fd_can_read,IOReadHandler * fd_read,IOEventHandler * fd_event,BackendChangeHandler * be_change,void * opaque,GMainContext * context,bool set_open)295 void qemu_chr_fe_set_handlers(CharBackend *b,
296                               IOCanReadHandler *fd_can_read,
297                               IOReadHandler *fd_read,
298                               IOEventHandler *fd_event,
299                               BackendChangeHandler *be_change,
300                               void *opaque,
301                               GMainContext *context,
302                               bool set_open)
303 {
304     qemu_chr_fe_set_handlers_full(b, fd_can_read, fd_read, fd_event, be_change,
305                                   opaque, context, set_open,
306                                   true);
307 }
308 
qemu_chr_fe_take_focus(CharBackend * b)309 void qemu_chr_fe_take_focus(CharBackend *b)
310 {
311     if (!b->chr) {
312         return;
313     }
314 
315     if (CHARDEV_IS_MUX(b->chr)) {
316         mux_set_focus(b->chr, b->tag);
317     }
318 }
319 
qemu_chr_fe_wait_connected(CharBackend * be,Error ** errp)320 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)
321 {
322     if (!be->chr) {
323         error_setg(errp, "missing associated backend");
324         return -1;
325     }
326 
327     return qemu_chr_wait_connected(be->chr, errp);
328 }
329 
qemu_chr_fe_set_echo(CharBackend * be,bool echo)330 void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
331 {
332     Chardev *chr = be->chr;
333 
334     if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) {
335         CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo);
336     }
337 }
338 
qemu_chr_fe_set_open(CharBackend * be,bool is_open)339 void qemu_chr_fe_set_open(CharBackend *be, bool is_open)
340 {
341     Chardev *chr = be->chr;
342 
343     if (!chr) {
344         return;
345     }
346 
347     if (be->fe_is_open == is_open) {
348         return;
349     }
350     be->fe_is_open = is_open;
351     if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) {
352         CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, is_open);
353     }
354 }
355 
qemu_chr_fe_add_watch(CharBackend * be,GIOCondition cond,FEWatchFunc func,void * user_data)356 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
357                             FEWatchFunc func, void *user_data)
358 {
359     Chardev *s = be->chr;
360     GSource *src;
361     guint tag;
362 
363     if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) {
364         return 0;
365     }
366 
367     src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond);
368     if (!src) {
369         return 0;
370     }
371 
372     g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
373     tag = g_source_attach(src, s->gcontext);
374     g_source_unref(src);
375 
376     return tag;
377 }
378 
qemu_chr_fe_disconnect(CharBackend * be)379 void qemu_chr_fe_disconnect(CharBackend *be)
380 {
381     Chardev *chr = be->chr;
382 
383     if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) {
384         CHARDEV_GET_CLASS(chr)->chr_disconnect(chr);
385     }
386 }
387