| 1 | #ifndef QEMU_CHAR_H |
| 2 | #define QEMU_CHAR_H |
| 3 | |
| 4 | #include "qapi/qapi-types-char.h" |
| 5 | #include "qemu/bitmap.h" |
| 6 | #include "qemu/thread.h" |
| 7 | #include "qom/object.h" |
| 8 | |
| 9 | #define IAC_EOR 239 |
| 10 | #define IAC_SE 240 |
| 11 | #define IAC_NOP 241 |
| 12 | #define IAC_BREAK 243 |
| 13 | #define IAC_IP 244 |
| 14 | #define IAC_SB 250 |
| 15 | #define IAC 255 |
| 16 | |
| 17 | /* character device */ |
| 18 | typedef struct CharFrontend CharFrontend; |
| 19 | |
| 20 | typedef enum { |
| 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. |
| 34 | */ |
| 35 | } QEMUChrEvent; |
| 36 | |
| 37 | #define CHR_READ_BUF_LEN 4096 |
| 38 | |
| 39 | typedef enum { |
| 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 |
| 43 | * valid connection */ |
| 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, |
| 53 | |
| 54 | QEMU_CHAR_FEATURE_LAST, |
| 55 | } ChardevFeature; |
| 56 | |
| 57 | #define qemu_chr_replay(chr) qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY) |
| 58 | |
| 59 | struct Chardev { |
| 60 | Object parent_obj; |
| 61 | |
| 62 | QemuMutex chr_write_lock; |
| 63 | CharFrontend *fe; |
| 64 | char *label; |
| 65 | int logfd; |
| 66 | bool logtimestamp; |
| 67 | bool log_line_start; |
| 68 | int be_open; |
| 69 | /* used to coordinate the chardev-change special-case: */ |
| 70 | bool handover_yank_instance; |
| 71 | GSource *gsource; |
| 72 | GMainContext *gcontext; |
| 73 | DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST); |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * qemu_chr_new_from_opts: |
| 78 | * @opts: see qemu-config.c for a list of valid options |
| 79 | * @context: the #GMainContext to be used at initialization time |
| 80 | * |
| 81 | * Create a new character backend from a QemuOpts list. |
| 82 | * |
| 83 | * Returns: on success: a new character backend |
| 84 | * otherwise: NULL; @errp specifies the error |
| 85 | * or left untouched in case of help option |
| 86 | */ |
| 87 | Chardev *qemu_chr_new_from_opts(QemuOpts *opts, |
| 88 | GMainContext *context, |
| 89 | Error **errp); |
| 90 | |
| 91 | /** |
| 92 | * qemu_chr_parse_common: |
| 93 | * @opts: the options that still need parsing |
| 94 | * @backend: a new backend |
| 95 | * |
| 96 | * Parse the common options available to all character backends. |
| 97 | */ |
| 98 | void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend); |
| 99 | |
| 100 | /** |
| 101 | * qemu_chr_parse_opts: |
| 102 | * |
| 103 | * Parse the options to the ChardevBackend struct. |
| 104 | * |
| 105 | * Returns: a new backend or NULL on error |
| 106 | */ |
| 107 | ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, |
| 108 | Error **errp); |
| 109 | |
| 110 | /** |
| 111 | * qemu_chr_new: |
| 112 | * @label: the name of the backend |
| 113 | * @filename: the URI |
| 114 | * @context: the #GMainContext to be used at initialization time |
| 115 | * |
| 116 | * Create a new character backend from a URI. |
| 117 | * Do not implicitly initialize a monitor if the chardev is muxed. |
| 118 | * |
| 119 | * Returns: a new character backend |
| 120 | */ |
| 121 | Chardev *qemu_chr_new(const char *label, const char *filename, |
| 122 | GMainContext *context); |
| 123 | |
| 124 | /** |
| 125 | * qemu_chr_new_mux_mon: |
| 126 | * @label: the name of the backend |
| 127 | * @filename: the URI |
| 128 | * @context: the #GMainContext to be used at initialization time |
| 129 | * |
| 130 | * Create a new character backend from a URI. |
| 131 | * Implicitly initialize a monitor if the chardev is muxed. |
| 132 | * |
| 133 | * Returns: a new character backend |
| 134 | */ |
| 135 | Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename, |
| 136 | GMainContext *context); |
| 137 | |
| 138 | /** |
| 139 | * qemu_chr_change: |
| 140 | * @opts: the new backend options |
| 141 | * |
| 142 | * Change an existing character backend |
| 143 | */ |
| 144 | void qemu_chr_change(QemuOpts *opts, Error **errp); |
| 145 | |
| 146 | /** |
| 147 | * qemu_chr_cleanup: |
| 148 | * |
| 149 | * Delete all chardevs (when leaving qemu) |
| 150 | */ |
| 151 | void qemu_chr_cleanup(void); |
| 152 | |
| 153 | /** |
| 154 | * qemu_chr_new_noreplay: |
| 155 | * @label: the name of the backend |
| 156 | * @filename: the URI |
| 157 | * @permit_mux_mon: if chardev is muxed, initialize a monitor |
| 158 | * @context: the #GMainContext to be used at initialization time |
| 159 | * |
| 160 | * Create a new character backend from a URI. |
| 161 | * Character device communications are not written |
| 162 | * into the replay log. |
| 163 | * |
| 164 | * Returns: a new character backend |
| 165 | */ |
| 166 | Chardev *qemu_chr_new_noreplay(const char *label, const char *filename, |
| 167 | bool permit_mux_mon, GMainContext *context); |
| 168 | |
| 169 | /** |
| 170 | * qemu_chr_be_can_write: |
| 171 | * |
| 172 | * Determine how much data the front end can currently accept. This function |
| 173 | * returns the number of bytes the front end can accept. If it returns 0, the |
| 174 | * front end cannot receive data at the moment. The function must be polled |
| 175 | * to determine when data can be received. |
| 176 | * |
| 177 | * Returns: the number of bytes the front end can receive via @qemu_chr_be_write |
| 178 | */ |
| 179 | int qemu_chr_be_can_write(Chardev *s); |
| 180 | |
| 181 | /** |
| 182 | * qemu_chr_be_write: |
| 183 | * @buf: a buffer to receive data from the front end |
| 184 | * @len: the number of bytes to receive from the front end |
| 185 | * |
| 186 | * Write data from the back end to the front end. Before issuing this call, |
| 187 | * the caller should call @qemu_chr_be_can_write to determine how much data |
| 188 | * the front end can currently accept. |
| 189 | */ |
| 190 | void qemu_chr_be_write(Chardev *s, const uint8_t *buf, int len); |
| 191 | |
| 192 | /** |
| 193 | * qemu_chr_be_write_impl: |
| 194 | * @buf: a buffer to receive data from the front end |
| 195 | * @len: the number of bytes to receive from the front end |
| 196 | * |
| 197 | * Implementation of back end writing. Used by replay module. |
| 198 | */ |
| 199 | void qemu_chr_be_write_impl(Chardev *s, const uint8_t *buf, int len); |
| 200 | |
| 201 | /** |
| 202 | * qemu_chr_be_update_read_handlers: |
| 203 | * @context: the gcontext that will be used to attach the watch sources |
| 204 | * |
| 205 | * Invoked when frontend read handlers are setup |
| 206 | */ |
| 207 | void qemu_chr_be_update_read_handlers(Chardev *s, |
| 208 | GMainContext *context); |
| 209 | |
| 210 | /** |
| 211 | * qemu_chr_be_event: |
| 212 | * @event: the event to send |
| 213 | * |
| 214 | * Send an event from the back end to the front end. |
| 215 | */ |
| 216 | void qemu_chr_be_event(Chardev *s, QEMUChrEvent event); |
| 217 | |
| 218 | int qemu_chr_add_client(Chardev *s, int fd); |
| 219 | Chardev *qemu_chr_find(const char *name); |
| 220 | |
| 221 | bool qemu_chr_has_feature(Chardev *chr, |
| 222 | ChardevFeature feature); |
| 223 | void qemu_chr_set_feature(Chardev *chr, |
| 224 | ChardevFeature feature); |
| 225 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename, |
| 226 | bool permit_mux_mon); |
| 227 | int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all); |
| 228 | #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true) |
| 229 | int qemu_chr_wait_connected(Chardev *chr, Error **errp); |
| 230 | |
| 231 | #define TYPE_CHARDEV "chardev" |
| 232 | OBJECT_DECLARE_TYPE(Chardev, ChardevClass, CHARDEV) |
| 233 | |
| 234 | #define TYPE_CHARDEV_NULL "chardev-null" |
| 235 | #define TYPE_CHARDEV_MUX "chardev-mux" |
| 236 | #define TYPE_CHARDEV_HUB "chardev-hub" |
| 237 | #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf" |
| 238 | #define TYPE_CHARDEV_PTY "chardev-pty" |
| 239 | #define TYPE_CHARDEV_CONSOLE "chardev-console" |
| 240 | #define TYPE_CHARDEV_STDIO "chardev-stdio" |
| 241 | #define TYPE_CHARDEV_PIPE "chardev-pipe" |
| 242 | #define TYPE_CHARDEV_MEMORY "chardev-memory" |
| 243 | #define TYPE_CHARDEV_PARALLEL "chardev-parallel" |
| 244 | #define TYPE_CHARDEV_FILE "chardev-file" |
| 245 | #define TYPE_CHARDEV_SERIAL "chardev-serial" |
| 246 | #define TYPE_CHARDEV_SOCKET "chardev-socket" |
| 247 | #define TYPE_CHARDEV_UDP "chardev-udp" |
| 248 | |
| 249 | #define CHARDEV_IS_RINGBUF(chr) \ |
| 250 | object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF) |
| 251 | |
| 252 | struct ChardevClass { |
| 253 | ObjectClass parent_class; |
| 254 | |
| 255 | bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */ |
| 256 | bool supports_yank; |
| 257 | bool supports_size_opts; |
| 258 | bool supports_encoding_opts; |
| 259 | |
| 260 | /* parse command line options and populate QAPI @backend */ |
| 261 | void (*chr_parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); |
| 262 | |
| 263 | /* called after construction, open/starts the backend */ |
| 264 | bool (*chr_open)(Chardev *chr, ChardevBackend *backend, Error **errp); |
| 265 | |
| 266 | /* write buf to the backend */ |
| 267 | int (*chr_write)(Chardev *s, const uint8_t *buf, int len); |
| 268 | |
| 269 | /* |
| 270 | * Read from the backend (blocking). A typical front-end will instead rely |
| 271 | * on chr_can_read/chr_read being called when polling/looping. |
| 272 | */ |
| 273 | int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len); |
| 274 | |
| 275 | /* create a watch on the backend */ |
| 276 | GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond); |
| 277 | |
| 278 | /* update the backend internal sources */ |
| 279 | void (*chr_update_read_handler)(Chardev *s); |
| 280 | |
| 281 | /* send an ioctl to the backend */ |
| 282 | int (*chr_ioctl)(Chardev *s, int cmd, void *arg); |
| 283 | |
| 284 | /* get ancillary-received fds during last read */ |
| 285 | int (*chr_get_msgfds)(Chardev *s, int* fds, int num); |
| 286 | |
| 287 | /* set ancillary fds to be sent with next write */ |
| 288 | int (*chr_set_msgfds)(Chardev *s, int *fds, int num); |
| 289 | |
| 290 | /* accept the given fd */ |
| 291 | int (*chr_add_client)(Chardev *chr, int fd); |
| 292 | |
| 293 | /* wait for a connection */ |
| 294 | int (*chr_wait_connected)(Chardev *chr, Error **errp); |
| 295 | |
| 296 | /* disconnect a connection */ |
| 297 | void (*chr_disconnect)(Chardev *chr); |
| 298 | |
| 299 | /* called by frontend when it can read */ |
| 300 | void (*chr_accept_input)(Chardev *chr); |
| 301 | |
| 302 | /* set terminal echo */ |
| 303 | void (*chr_set_echo)(Chardev *chr, bool echo); |
| 304 | |
| 305 | /* notify the backend of frontend open state */ |
| 306 | void (*chr_set_fe_open)(Chardev *chr, int fe_open); |
| 307 | |
| 308 | /* handle various events */ |
| 309 | void (*chr_be_event)(Chardev *s, QEMUChrEvent event); |
| 310 | |
| 311 | void (*chr_listener_cleanup)(Chardev *chr); |
| 312 | |
| 313 | /* return PTY name if available */ |
| 314 | char *(*chr_get_pty_name)(Chardev *s); |
| 315 | |
| 316 | /* get filename for reporting */ |
| 317 | char *(*chr_get_filename)(Chardev *s); |
| 318 | }; |
| 319 | |
| 320 | Chardev *qemu_chardev_new(const char *id, const char *typename, |
| 321 | ChardevBackend *backend, GMainContext *context, |
| 322 | Error **errp); |
| 323 | |
| 324 | extern int term_escape_char; |
| 325 | |
| 326 | GSource *qemu_chr_timeout_add_ms(Chardev *chr, guint ms, |
| 327 | GSourceFunc func, void *private); |
| 328 | |
| 329 | void suspend_mux_open(void); |
| 330 | void resume_mux_open(void); |
| 331 | |
| 332 | char *qemu_chr_get_pty_name(Chardev *chr); |
| 333 | char *qemu_chr_get_filename(Chardev *chr); |
| 334 | |
| 335 | #define CHARDEV_VC_ENCODING_PROPERTY_DEFINE(cast_func) \ |
| 336 | static int get_encoding(Object *obj, Error **errp) \ |
| 337 | { \ |
| 338 | return cast_func(obj)->encoding; \ |
| 339 | } \ |
| 340 | \ |
| 341 | static void set_encoding(Object *obj, int value, Error **errp) \ |
| 342 | { \ |
| 343 | cast_func(obj)->encoding = value; \ |
| 344 | } |
| 345 | |
| 346 | static inline void chardev_vc_add_encoding_prop(ObjectClass *oc, |
| 347 | int (*get)(Object *, Error **), |
| 348 | void (*set)(Object *, int, Error **)) |
| 349 | { |
| 350 | object_class_property_add_enum(oc, "encoding", "ChardevVCEncoding", |
| 351 | &ChardevVCEncoding_lookup, get, set); |
| 352 | } |
| 353 | |
| 354 | #endif |