4 #include "qapi/qapi-types-char.h"
5 #include "qemu/bitmap.h"
6 #include "qemu/thread.h"
7 #include "qom/object.h"
17 /* character device */
18 typedef struct CharBackend CharBackend
;
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. NOTE: currently this event
26 * is only bound to the read port of the chardev.
27 * Normally the read port and write port of a
28 * chardev should be the same, but it can be
29 * different, e.g., for fd chardevs, when the two
30 * fds are different. So when we received the
31 * CLOSED event it's still possible that the out
32 * port is still open. TODO: we should only send
33 * the CLOSED event when both ports are closed.
37 #define CHR_READ_BUF_LEN 4096
40 /* Whether the chardev peer is able to close and
41 * reopen the data channel, thus requiring support
42 * for qemu_chr_wait_connected() to wait for a
44 QEMU_CHAR_FEATURE_RECONNECTABLE
,
45 /* Whether it is possible to send/recv file descriptors
46 * over the data channel */
47 QEMU_CHAR_FEATURE_FD_PASS
,
48 /* Whether replay or record mode is enabled */
49 QEMU_CHAR_FEATURE_REPLAY
,
50 /* Whether the gcontext can be changed after calling
51 * qemu_chr_be_update_read_handlers() */
52 QEMU_CHAR_FEATURE_GCONTEXT
,
54 QEMU_CHAR_FEATURE_LAST
,
57 #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY)
62 QemuMutex chr_write_lock
;
68 /* used to coordinate the chardev-change special-case: */
69 bool handover_yank_instance
;
71 GMainContext
*gcontext
;
72 DECLARE_BITMAP(features
, QEMU_CHAR_FEATURE_LAST
);
76 * qemu_chr_new_from_opts:
77 * @opts: see qemu-config.c for a list of valid options
78 * @context: the #GMainContext to be used at initialization time
80 * Create a new character backend from a QemuOpts list.
82 * Returns: on success: a new character backend
83 * otherwise: NULL; @errp specifies the error
84 * or left untouched in case of help option
86 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
,
87 GMainContext
*context
,
91 * qemu_chr_parse_common:
92 * @opts: the options that still need parsing
93 * @backend: a new backend
95 * Parse the common options available to all character backends.
97 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
);
100 * qemu_chr_parse_opts:
102 * Parse the options to the ChardevBackend struct.
104 * Returns: a new backend or NULL on error
106 ChardevBackend
*qemu_chr_parse_opts(QemuOpts
*opts
,
111 * @label: the name of the backend
113 * @context: the #GMainContext to be used at initialization time
115 * Create a new character backend from a URI.
116 * Do not implicitly initialize a monitor if the chardev is muxed.
118 * Returns: a new character backend
120 Chardev
*qemu_chr_new(const char *label
, const char *filename
,
121 GMainContext
*context
);
124 * qemu_chr_new_mux_mon:
125 * @label: the name of the backend
127 * @context: the #GMainContext to be used at initialization time
129 * Create a new character backend from a URI.
130 * Implicitly initialize a monitor if the chardev is muxed.
132 * Returns: a new character backend
134 Chardev
*qemu_chr_new_mux_mon(const char *label
, const char *filename
,
135 GMainContext
*context
);
139 * @opts: the new backend options
141 * Change an existing character backend
143 void qemu_chr_change(QemuOpts
*opts
, Error
**errp
);
148 * Delete all chardevs (when leaving qemu)
150 void qemu_chr_cleanup(void);
153 * qemu_chr_new_noreplay:
154 * @label: the name of the backend
156 * @permit_mux_mon: if chardev is muxed, initialize a monitor
157 * @context: the #GMainContext to be used at initialization time
159 * Create a new character backend from a URI.
160 * Character device communications are not written
161 * into the replay log.
163 * Returns: a new character backend
165 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
,
166 bool permit_mux_mon
, GMainContext
*context
);
169 * qemu_chr_be_can_write:
171 * Determine how much data the front end can currently accept. This function
172 * returns the number of bytes the front end can accept. If it returns 0, the
173 * front end cannot receive data at the moment. The function must be polled
174 * to determine when data can be received.
176 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
178 int qemu_chr_be_can_write(Chardev
*s
);
182 * @buf: a buffer to receive data from the front end
183 * @len: the number of bytes to receive from the front end
185 * Write data from the back end to the front end. Before issuing this call,
186 * the caller should call @qemu_chr_be_can_write to determine how much data
187 * the front end can currently accept.
189 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
);
192 * qemu_chr_be_write_impl:
193 * @buf: a buffer to receive data from the front end
194 * @len: the number of bytes to receive from the front end
196 * Implementation of back end writing. Used by replay module.
198 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
);
201 * qemu_chr_be_update_read_handlers:
202 * @context: the gcontext that will be used to attach the watch sources
204 * Invoked when frontend read handlers are setup
206 void qemu_chr_be_update_read_handlers(Chardev
*s
,
207 GMainContext
*context
);
211 * @event: the event to send
213 * Send an event from the back end to the front end.
215 void qemu_chr_be_event(Chardev
*s
, QEMUChrEvent event
);
217 int qemu_chr_add_client(Chardev
*s
, int fd
);
218 Chardev
*qemu_chr_find(const char *name
);
220 bool qemu_chr_has_feature(Chardev
*chr
,
221 ChardevFeature feature
);
222 void qemu_chr_set_feature(Chardev
*chr
,
223 ChardevFeature feature
);
224 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
,
225 bool permit_mux_mon
);
226 int qemu_chr_write(Chardev
*s
, const uint8_t *buf
, int len
, bool write_all
);
227 #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true)
228 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
);
230 #define TYPE_CHARDEV "chardev"
231 OBJECT_DECLARE_TYPE(Chardev
, ChardevClass
, CHARDEV
)
233 #define TYPE_CHARDEV_NULL "chardev-null"
234 #define TYPE_CHARDEV_MUX "chardev-mux"
235 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
236 #define TYPE_CHARDEV_PTY "chardev-pty"
237 #define TYPE_CHARDEV_CONSOLE "chardev-console"
238 #define TYPE_CHARDEV_STDIO "chardev-stdio"
239 #define TYPE_CHARDEV_PIPE "chardev-pipe"
240 #define TYPE_CHARDEV_MEMORY "chardev-memory"
241 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
242 #define TYPE_CHARDEV_FILE "chardev-file"
243 #define TYPE_CHARDEV_SERIAL "chardev-serial"
244 #define TYPE_CHARDEV_SOCKET "chardev-socket"
245 #define TYPE_CHARDEV_UDP "chardev-udp"
247 #define CHARDEV_IS_RINGBUF(chr) \
248 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
249 #define CHARDEV_IS_PTY(chr) \
250 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
252 struct ChardevClass
{
253 ObjectClass parent_class
;
255 bool internal
; /* TODO: eventually use TYPE_USER_CREATABLE */
258 /* parse command line options and populate QAPI @backend */
259 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
261 /* called after construction, open/starts the backend */
262 void (*open
)(Chardev
*chr
, ChardevBackend
*backend
,
263 bool *be_opened
, Error
**errp
);
265 /* write buf to the backend */
266 int (*chr_write
)(Chardev
*s
, const uint8_t *buf
, int len
);
269 * Read from the backend (blocking). A typical front-end will instead rely
270 * on chr_can_read/chr_read being called when polling/looping.
272 int (*chr_sync_read
)(Chardev
*s
, const uint8_t *buf
, int len
);
274 /* create a watch on the backend */
275 GSource
*(*chr_add_watch
)(Chardev
*s
, GIOCondition cond
);
277 /* update the backend internal sources */
278 void (*chr_update_read_handler
)(Chardev
*s
);
280 /* send an ioctl to the backend */
281 int (*chr_ioctl
)(Chardev
*s
, int cmd
, void *arg
);
283 /* get ancillary-received fds during last read */
284 int (*get_msgfds
)(Chardev
*s
, int* fds
, int num
);
286 /* set ancillary fds to be sent with next write */
287 int (*set_msgfds
)(Chardev
*s
, int *fds
, int num
);
289 /* accept the given fd */
290 int (*chr_add_client
)(Chardev
*chr
, int fd
);
292 /* wait for a connection */
293 int (*chr_wait_connected
)(Chardev
*chr
, Error
**errp
);
295 /* disconnect a connection */
296 void (*chr_disconnect
)(Chardev
*chr
);
298 /* called by frontend when it can read */
299 void (*chr_accept_input
)(Chardev
*chr
);
301 /* set terminal echo */
302 void (*chr_set_echo
)(Chardev
*chr
, bool echo
);
304 /* notify the backend of frontend open state */
305 void (*chr_set_fe_open
)(Chardev
*chr
, int fe_open
);
307 /* handle various events */
308 void (*chr_be_event
)(Chardev
*s
, QEMUChrEvent event
);
311 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
312 ChardevBackend
*backend
, GMainContext
*context
,
315 extern int term_escape_char
;
317 GSource
*qemu_chr_timeout_add_ms(Chardev
*chr
, guint ms
,
318 GSourceFunc func
, void *private);
320 void suspend_mux_open(void);
321 void resume_mux_open(void);
324 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);