4 #include "qemu-common.h"
5 #include "qemu/option.h"
6 #include "qemu/main-loop.h"
7 #include "qemu/bitmap.h"
8 #include "qom/object.h"
18 /* character device */
21 CHR_EVENT_BREAK
, /* serial break char */
22 CHR_EVENT_OPENED
, /* new connection established */
23 CHR_EVENT_MUX_IN
, /* mux-focus was set to this terminal */
24 CHR_EVENT_MUX_OUT
, /* mux-focus will move on */
25 CHR_EVENT_CLOSED
/* connection closed */
28 #define CHR_READ_BUF_LEN 4096
30 typedef void IOEventHandler(void *opaque
, int event
);
33 /* Whether the chardev peer is able to close and
34 * reopen the data channel, thus requiring support
35 * for qemu_chr_wait_connected() to wait for a
37 QEMU_CHAR_FEATURE_RECONNECTABLE
,
38 /* Whether it is possible to send/recv file descriptors
39 * over the data channel */
40 QEMU_CHAR_FEATURE_FD_PASS
,
41 /* Whether replay or record mode is enabled */
42 QEMU_CHAR_FEATURE_REPLAY
,
44 QEMU_CHAR_FEATURE_LAST
,
47 /* This is the backend as seen by frontend, the actual backend is
49 typedef struct CharBackend
{
51 IOEventHandler
*chr_event
;
52 IOCanReadHandler
*chr_can_read
;
53 IOReadHandler
*chr_read
;
62 QemuMutex chr_write_lock
;
69 DECLARE_BITMAP(features
, QEMU_CHAR_FEATURE_LAST
);
73 * @qemu_chr_new_from_opts:
75 * Create a new character backend from a QemuOpts list.
77 * @opts see qemu-config.c for a list of valid options
79 * Returns: a new character backend
81 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
,
85 * @qemu_chr_parse_common:
87 * Parse the common options available to all character backends.
89 * @opts the options that still need parsing
90 * @backend a new backend
92 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
);
97 * Create a new character backend from a URI.
99 * @label the name of the backend
102 * Returns: a new character backend
104 Chardev
*qemu_chr_new(const char *label
, const char *filename
);
108 * @qemu_chr_fe_disconnect:
110 * Close a fd accpeted by character backend.
111 * Without associated Chardev, do nothing.
113 void qemu_chr_fe_disconnect(CharBackend
*be
);
118 * Delete all chardevs (when leaving qemu)
120 void qemu_chr_cleanup(void);
123 * @qemu_chr_fe_wait_connected:
125 * Wait for characted backend to be connected, return < 0 on error or
126 * if no assicated Chardev.
128 int qemu_chr_fe_wait_connected(CharBackend
*be
, Error
**errp
);
131 * @qemu_chr_new_noreplay:
133 * Create a new character backend from a URI.
134 * Character device communications are not written
135 * into the replay log.
137 * @label the name of the backend
140 * Returns: a new character backend
142 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
);
145 * @qemu_chr_fe_set_echo:
147 * Ask the backend to override its normal echo setting. This only really
148 * applies to the stdio backend and is used by the QMP server such that you
149 * can see what you type if you try to type QMP commands.
150 * Without associated Chardev, do nothing.
152 * @echo true to enable echo, false to disable echo
154 void qemu_chr_fe_set_echo(CharBackend
*be
, bool echo
);
157 * @qemu_chr_fe_set_open:
159 * Set character frontend open status. This is an indication that the
160 * front end is ready (or not) to begin doing I/O.
161 * Without associated Chardev, do nothing.
163 void qemu_chr_fe_set_open(CharBackend
*be
, int fe_open
);
166 * @qemu_chr_fe_printf:
168 * Write to a character backend using a printf style interface. This
169 * function is thread-safe. It does nothing without associated
174 void qemu_chr_fe_printf(CharBackend
*be
, const char *fmt
, ...)
178 * @qemu_chr_fe_add_watch:
180 * If the backend is connected, create and add a #GSource that fires
181 * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP)
182 * is active; return the #GSource's tag. If it is disconnected,
183 * or without associated Chardev, return 0.
185 * @cond the condition to poll for
186 * @func the function to call when the condition happens
187 * @user_data the opaque pointer to pass to @func
189 * Returns: the source tag
191 guint
qemu_chr_fe_add_watch(CharBackend
*be
, GIOCondition cond
,
192 GIOFunc func
, void *user_data
);
195 * @qemu_chr_fe_write:
197 * Write data to a character backend from the front end. This function
198 * will send data from the front end to the back end. This function
202 * @len the number of bytes to send
204 * Returns: the number of bytes consumed (0 if no assicated Chardev)
206 int qemu_chr_fe_write(CharBackend
*be
, const uint8_t *buf
, int len
);
209 * @qemu_chr_fe_write_all:
211 * Write data to a character backend from the front end. This function will
212 * send data from the front end to the back end. Unlike @qemu_chr_fe_write,
213 * this function will block if the back end cannot consume all of the data
214 * attempted to be written. This function is thread-safe.
217 * @len the number of bytes to send
219 * Returns: the number of bytes consumed (0 if no assicated Chardev)
221 int qemu_chr_fe_write_all(CharBackend
*be
, const uint8_t *buf
, int len
);
224 * @qemu_chr_fe_read_all:
226 * Read data to a buffer from the back end.
228 * @buf the data buffer
229 * @len the number of bytes to read
231 * Returns: the number of bytes read (0 if no assicated Chardev)
233 int qemu_chr_fe_read_all(CharBackend
*be
, uint8_t *buf
, int len
);
236 * @qemu_chr_fe_ioctl:
238 * Issue a device specific ioctl to a backend. This function is thread-safe.
240 * @cmd see CHR_IOCTL_*
241 * @arg the data associated with @cmd
243 * Returns: if @cmd is not supported by the backend or there is no
244 * associated Chardev, -ENOTSUP, otherwise the return
245 * value depends on the semantics of @cmd
247 int qemu_chr_fe_ioctl(CharBackend
*be
, int cmd
, void *arg
);
250 * @qemu_chr_fe_get_msgfd:
252 * For backends capable of fd passing, return the latest file descriptor passed
255 * Returns: -1 if fd passing isn't supported or there is no pending file
256 * descriptor. If a file descriptor is returned, subsequent calls to
257 * this function will return -1 until a client sends a new file
260 int qemu_chr_fe_get_msgfd(CharBackend
*be
);
263 * @qemu_chr_fe_get_msgfds:
265 * For backends capable of fd passing, return the number of file received
266 * descriptors and fills the fds array up to num elements
268 * Returns: -1 if fd passing isn't supported or there are no pending file
269 * descriptors. If file descriptors are returned, subsequent calls to
270 * this function will return -1 until a client sends a new set of file
273 int qemu_chr_fe_get_msgfds(CharBackend
*be
, int *fds
, int num
);
276 * @qemu_chr_fe_set_msgfds:
278 * For backends capable of fd passing, set an array of fds to be passed with
279 * the next send operation.
280 * A subsequent call to this function before calling a write function will
281 * result in overwriting the fd array with the new value without being send.
282 * Upon writing the message the fd array is freed.
284 * Returns: -1 if fd passing isn't supported or no associated Chardev.
286 int qemu_chr_fe_set_msgfds(CharBackend
*be
, int *fds
, int num
);
289 * @qemu_chr_be_can_write:
291 * Determine how much data the front end can currently accept. This function
292 * returns the number of bytes the front end can accept. If it returns 0, the
293 * front end cannot receive data at the moment. The function must be polled
294 * to determine when data can be received.
296 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
298 int qemu_chr_be_can_write(Chardev
*s
);
301 * @qemu_chr_be_write:
303 * Write data from the back end to the front end. Before issuing this call,
304 * the caller should call @qemu_chr_be_can_write to determine how much data
305 * the front end can currently accept.
307 * @buf a buffer to receive data from the front end
308 * @len the number of bytes to receive from the front end
310 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
);
313 * @qemu_chr_be_write_impl:
315 * Implementation of back end writing. Used by replay module.
317 * @buf a buffer to receive data from the front end
318 * @len the number of bytes to receive from the front end
320 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
);
323 * @qemu_chr_be_event:
325 * Send an event from the back end to the front end.
327 * @event the event to send
329 void qemu_chr_be_event(Chardev
*s
, int event
);
334 * Initializes a front end for the given CharBackend and
335 * Chardev. Call qemu_chr_fe_deinit() to remove the association and
336 * release the driver.
338 * Returns: false on error.
340 bool qemu_chr_fe_init(CharBackend
*b
, Chardev
*s
, Error
**errp
);
343 * @qemu_chr_fe_get_driver:
345 * Returns the driver associated with a CharBackend or NULL if no
346 * associated Chardev.
348 Chardev
*qemu_chr_fe_get_driver(CharBackend
*be
);
351 * @qemu_chr_fe_deinit:
353 * Dissociate the CharBackend from the Chardev.
355 * Safe to call without associated Chardev.
357 void qemu_chr_fe_deinit(CharBackend
*b
);
360 * @qemu_chr_fe_set_handlers:
362 * @fd_can_read: callback to get the amount of data the frontend may
364 * @fd_read: callback to receive data from char
365 * @fd_event: event callback
366 * @opaque: an opaque pointer for the callbacks
367 * @context: a main loop context or NULL for the default
368 * @set_open: whether to call qemu_chr_fe_set_open() implicitely when
369 * any of the handler is non-NULL
371 * Set the front end char handlers. The front end takes the focus if
372 * any of the handler is non-NULL.
374 * Without associated Chardev, nothing is changed.
376 void qemu_chr_fe_set_handlers(CharBackend
*b
,
377 IOCanReadHandler
*fd_can_read
,
378 IOReadHandler
*fd_read
,
379 IOEventHandler
*fd_event
,
381 GMainContext
*context
,
385 * @qemu_chr_fe_take_focus:
387 * Take the focus (if the front end is muxed).
389 * Without associated Chardev, nothing is changed.
391 void qemu_chr_fe_take_focus(CharBackend
*b
);
393 void qemu_chr_fe_accept_input(CharBackend
*be
);
394 int qemu_chr_add_client(Chardev
*s
, int fd
);
395 Chardev
*qemu_chr_find(const char *name
);
397 bool qemu_chr_has_feature(Chardev
*chr
,
398 ChardevFeature feature
);
399 void qemu_chr_set_feature(Chardev
*chr
,
400 ChardevFeature feature
);
401 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
);
402 int qemu_chr_write_all(Chardev
*s
, const uint8_t *buf
, int len
);
403 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
);
405 #define TYPE_CHARDEV "chardev"
406 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
407 #define CHARDEV_CLASS(klass) \
408 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
409 #define CHARDEV_GET_CLASS(obj) \
410 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
412 #define TYPE_CHARDEV_NULL "chardev-null"
413 #define TYPE_CHARDEV_MUX "chardev-mux"
414 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
415 #define TYPE_CHARDEV_PTY "chardev-pty"
416 #define TYPE_CHARDEV_CONSOLE "chardev-console"
417 #define TYPE_CHARDEV_STDIO "chardev-stdio"
418 #define TYPE_CHARDEV_PIPE "chardev-pipe"
419 #define TYPE_CHARDEV_MEMORY "chardev-memory"
420 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
421 #define TYPE_CHARDEV_FILE "chardev-file"
422 #define TYPE_CHARDEV_SERIAL "chardev-serial"
423 #define TYPE_CHARDEV_SOCKET "chardev-socket"
424 #define TYPE_CHARDEV_UDP "chardev-udp"
426 #define CHARDEV_IS_RINGBUF(chr) \
427 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
428 #define CHARDEV_IS_PTY(chr) \
429 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
431 typedef struct ChardevClass
{
432 ObjectClass parent_class
;
434 bool internal
; /* TODO: eventually use TYPE_USER_CREATABLE */
435 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
437 void (*open
)(Chardev
*chr
, ChardevBackend
*backend
,
438 bool *be_opened
, Error
**errp
);
440 int (*chr_write
)(Chardev
*s
, const uint8_t *buf
, int len
);
441 int (*chr_sync_read
)(Chardev
*s
, const uint8_t *buf
, int len
);
442 GSource
*(*chr_add_watch
)(Chardev
*s
, GIOCondition cond
);
443 void (*chr_update_read_handler
)(Chardev
*s
, GMainContext
*context
);
444 int (*chr_ioctl
)(Chardev
*s
, int cmd
, void *arg
);
445 int (*get_msgfds
)(Chardev
*s
, int* fds
, int num
);
446 int (*set_msgfds
)(Chardev
*s
, int *fds
, int num
);
447 int (*chr_add_client
)(Chardev
*chr
, int fd
);
448 int (*chr_wait_connected
)(Chardev
*chr
, Error
**errp
);
449 void (*chr_disconnect
)(Chardev
*chr
);
450 void (*chr_accept_input
)(Chardev
*chr
);
451 void (*chr_set_echo
)(Chardev
*chr
, bool echo
);
452 void (*chr_set_fe_open
)(Chardev
*chr
, int fe_open
);
455 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
456 ChardevBackend
*backend
, Error
**errp
);
458 extern int term_escape_char
;
461 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);