| 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 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qapi/error.h" |
| 27 | #include "chardev/char.h" |
| 28 | #include "io/channel-file.h" |
| 29 | #include "qemu/sockets.h" |
| 30 | #include "qemu/error-report.h" |
| 31 | #include "qemu/module.h" |
| 32 | #include "qemu/option.h" |
| 33 | #include "qemu/qemu-print.h" |
| 34 | |
| 35 | #include "chardev/char-io.h" |
| 36 | #include "qom/object.h" |
| 37 | |
| 38 | struct PtyChardev { |
| 39 | Chardev parent; |
| 40 | QIOChannel *ioc; |
| 41 | int read_bytes; |
| 42 | |
| 43 | int connected; |
| 44 | GSource *timer_src; |
| 45 | char *path; |
| 46 | char *pty_name; |
| 47 | }; |
| 48 | typedef struct PtyChardev PtyChardev; |
| 49 | |
| 50 | DECLARE_INSTANCE_CHECKER(PtyChardev, PTY_CHARDEV, |
| 51 | TYPE_CHARDEV_PTY) |
| 52 | |
| 53 | static void pty_chr_state(Chardev *chr, int connected); |
| 54 | |
| 55 | static void pty_chr_timer_cancel(PtyChardev *s) |
| 56 | { |
| 57 | if (s->timer_src) { |
| 58 | g_source_destroy(s->timer_src); |
| 59 | g_source_unref(s->timer_src); |
| 60 | s->timer_src = NULL; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static gboolean pty_chr_timer(gpointer opaque) |
| 65 | { |
| 66 | struct Chardev *chr = CHARDEV(opaque); |
| 67 | PtyChardev *s = PTY_CHARDEV(opaque); |
| 68 | |
| 69 | pty_chr_timer_cancel(s); |
| 70 | if (!s->connected) { |
| 71 | /* Next poll ... */ |
| 72 | qemu_chr_be_update_read_handlers(chr, chr->gcontext); |
| 73 | } |
| 74 | return FALSE; |
| 75 | } |
| 76 | |
| 77 | static void pty_chr_rearm_timer(Chardev *chr, int ms) |
| 78 | { |
| 79 | PtyChardev *s = PTY_CHARDEV(chr); |
| 80 | char *name; |
| 81 | |
| 82 | pty_chr_timer_cancel(s); |
| 83 | name = g_strdup_printf("pty-timer-%s", chr->label); |
| 84 | s->timer_src = qemu_chr_timeout_add_ms(chr, ms, pty_chr_timer, chr); |
| 85 | g_source_set_name(s->timer_src, name); |
| 86 | g_free(name); |
| 87 | } |
| 88 | |
| 89 | static void pty_chr_update_read_handler(Chardev *chr) |
| 90 | { |
| 91 | PtyChardev *s = PTY_CHARDEV(chr); |
| 92 | GPollFD pfd; |
| 93 | int rc; |
| 94 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc); |
| 95 | |
| 96 | pfd.fd = fioc->fd; |
| 97 | pfd.events = G_IO_OUT; |
| 98 | pfd.revents = 0; |
| 99 | rc = RETRY_ON_EINTR(g_poll(&pfd, 1, 0)); |
| 100 | assert(rc >= 0); |
| 101 | |
| 102 | if (pfd.revents & G_IO_HUP) { |
| 103 | pty_chr_state(chr, 0); |
| 104 | } else { |
| 105 | pty_chr_state(chr, 1); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static int pty_chr_write(Chardev *chr, const uint8_t *buf, int len) |
| 110 | { |
| 111 | PtyChardev *s = PTY_CHARDEV(chr); |
| 112 | GPollFD pfd; |
| 113 | int rc; |
| 114 | |
| 115 | if (s->connected) { |
| 116 | return io_channel_send(s->ioc, buf, len); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * The other side might already be re-connected, but the timer might |
| 121 | * not have fired yet. So let's check here whether we can write again: |
| 122 | */ |
| 123 | pfd.fd = QIO_CHANNEL_FILE(s->ioc)->fd; |
| 124 | pfd.events = G_IO_OUT; |
| 125 | pfd.revents = 0; |
| 126 | rc = RETRY_ON_EINTR(g_poll(&pfd, 1, 0)); |
| 127 | g_assert(rc >= 0); |
| 128 | if (!(pfd.revents & G_IO_HUP) && (pfd.revents & G_IO_OUT)) { |
| 129 | return io_channel_send(s->ioc, buf, len); |
| 130 | } |
| 131 | |
| 132 | return len; |
| 133 | } |
| 134 | |
| 135 | static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond) |
| 136 | { |
| 137 | PtyChardev *s = PTY_CHARDEV(chr); |
| 138 | if (!s->connected) { |
| 139 | return NULL; |
| 140 | } |
| 141 | return qio_channel_create_watch(s->ioc, cond); |
| 142 | } |
| 143 | |
| 144 | static int pty_chr_read_poll(void *opaque) |
| 145 | { |
| 146 | Chardev *chr = CHARDEV(opaque); |
| 147 | PtyChardev *s = PTY_CHARDEV(opaque); |
| 148 | |
| 149 | s->read_bytes = qemu_chr_be_can_write(chr); |
| 150 | return s->read_bytes; |
| 151 | } |
| 152 | |
| 153 | static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) |
| 154 | { |
| 155 | Chardev *chr = CHARDEV(opaque); |
| 156 | PtyChardev *s = PTY_CHARDEV(opaque); |
| 157 | gsize len; |
| 158 | QEMU_UNINITIALIZED uint8_t buf[CHR_READ_BUF_LEN]; |
| 159 | ssize_t ret; |
| 160 | |
| 161 | len = sizeof(buf); |
| 162 | if (len > s->read_bytes) { |
| 163 | len = s->read_bytes; |
| 164 | } |
| 165 | if (len == 0) { |
| 166 | return TRUE; |
| 167 | } |
| 168 | ret = qio_channel_read(s->ioc, (char *)buf, len, NULL); |
| 169 | if (ret <= 0) { |
| 170 | pty_chr_state(chr, 0); |
| 171 | return FALSE; |
| 172 | } else { |
| 173 | pty_chr_state(chr, 1); |
| 174 | qemu_chr_be_write(chr, buf, ret); |
| 175 | } |
| 176 | return TRUE; |
| 177 | } |
| 178 | |
| 179 | static void pty_chr_state(Chardev *chr, int connected) |
| 180 | { |
| 181 | PtyChardev *s = PTY_CHARDEV(chr); |
| 182 | |
| 183 | if (!connected) { |
| 184 | remove_fd_in_watch(chr); |
| 185 | if (s->connected) { |
| 186 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
| 187 | } |
| 188 | s->connected = 0; |
| 189 | /* (re-)connect poll interval for idle guests: once per second. |
| 190 | * We check more frequently in case the guests sends data to |
| 191 | * the virtual device linked to our pty. */ |
| 192 | pty_chr_rearm_timer(chr, 1000); |
| 193 | } else { |
| 194 | pty_chr_timer_cancel(s); |
| 195 | if (!s->connected) { |
| 196 | s->connected = 1; |
| 197 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 198 | } |
| 199 | if (!chr->gsource) { |
| 200 | chr->gsource = io_add_watch_poll(chr, s->ioc, |
| 201 | pty_chr_read_poll, |
| 202 | pty_chr_read, |
| 203 | chr, chr->gcontext); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void char_pty_finalize(Object *obj) |
| 209 | { |
| 210 | Chardev *chr = CHARDEV(obj); |
| 211 | PtyChardev *s = PTY_CHARDEV(obj); |
| 212 | |
| 213 | /* unlink symlink */ |
| 214 | if (s->path) { |
| 215 | unlink(s->path); |
| 216 | g_free(s->path); |
| 217 | } |
| 218 | |
| 219 | pty_chr_state(chr, 0); |
| 220 | object_unref(OBJECT(s->ioc)); |
| 221 | pty_chr_timer_cancel(s); |
| 222 | } |
| 223 | |
| 224 | #if defined HAVE_PTY_H |
| 225 | # include <pty.h> |
| 226 | #elif defined CONFIG_BSD |
| 227 | # include <termios.h> |
| 228 | # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
| 229 | # include <libutil.h> |
| 230 | # else |
| 231 | # include <util.h> |
| 232 | # endif |
| 233 | #elif defined CONFIG_SOLARIS |
| 234 | # include <termios.h> |
| 235 | # include <stropts.h> |
| 236 | #else |
| 237 | # include <termios.h> |
| 238 | #endif |
| 239 | |
| 240 | #ifdef __sun__ |
| 241 | |
| 242 | #if !defined(HAVE_OPENPTY) |
| 243 | /* Once illumos has openpty(), this is going to be removed. */ |
| 244 | static int openpty(int *amaster, int *aslave, char *name, |
| 245 | struct termios *termp, struct winsize *winp) |
| 246 | { |
| 247 | const char *slave; |
| 248 | int mfd = -1, sfd = -1; |
| 249 | |
| 250 | *amaster = *aslave = -1; |
| 251 | |
| 252 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY); |
| 253 | if (mfd < 0) { |
| 254 | goto err; |
| 255 | } |
| 256 | |
| 257 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) { |
| 258 | goto err; |
| 259 | } |
| 260 | |
| 261 | if ((slave = ptsname(mfd)) == NULL) { |
| 262 | goto err; |
| 263 | } |
| 264 | |
| 265 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) { |
| 266 | goto err; |
| 267 | } |
| 268 | |
| 269 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
| 270 | (termp != NULL && tcgetattr(sfd, termp) < 0)) { |
| 271 | goto err; |
| 272 | } |
| 273 | |
| 274 | *amaster = mfd; |
| 275 | *aslave = sfd; |
| 276 | |
| 277 | if (winp) { |
| 278 | ioctl(sfd, TIOCSWINSZ, winp); |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | |
| 283 | err: |
| 284 | if (sfd != -1) { |
| 285 | close(sfd); |
| 286 | } |
| 287 | close(mfd); |
| 288 | return -1; |
| 289 | } |
| 290 | #endif |
| 291 | |
| 292 | static void cfmakeraw (struct termios *termios_p) |
| 293 | { |
| 294 | termios_p->c_iflag &= |
| 295 | ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); |
| 296 | termios_p->c_oflag &= ~OPOST; |
| 297 | termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); |
| 298 | termios_p->c_cflag &= ~(CSIZE | PARENB); |
| 299 | termios_p->c_cflag |= CS8; |
| 300 | |
| 301 | termios_p->c_cc[VMIN] = 0; |
| 302 | termios_p->c_cc[VTIME] = 0; |
| 303 | } |
| 304 | #endif |
| 305 | |
| 306 | /* like openpty() but also makes it raw; return master fd */ |
| 307 | static int qemu_openpty_raw(int *aslave, char **pty_name) |
| 308 | { |
| 309 | int amaster; |
| 310 | struct termios tty; |
| 311 | #if defined(__OpenBSD__) || defined(__DragonFly__) |
| 312 | char pty_buf[PATH_MAX]; |
| 313 | #define q_ptsname(x) pty_buf |
| 314 | #else |
| 315 | char *pty_buf = NULL; |
| 316 | #define q_ptsname(x) ptsname(x) |
| 317 | #endif |
| 318 | |
| 319 | if (openpty(&amaster, aslave, pty_buf, NULL, NULL) < 0) { |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | /* Set raw attributes on the pty. */ |
| 324 | tcgetattr(*aslave, &tty); |
| 325 | cfmakeraw(&tty); |
| 326 | tcsetattr(*aslave, TCSAFLUSH, &tty); |
| 327 | |
| 328 | *pty_name = g_strdup(q_ptsname(amaster)); |
| 329 | |
| 330 | return amaster; |
| 331 | } |
| 332 | |
| 333 | static bool pty_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) |
| 334 | { |
| 335 | PtyChardev *s; |
| 336 | int master_fd, slave_fd; |
| 337 | char *name; |
| 338 | char *path = backend->u.pty.data->path; |
| 339 | |
| 340 | s = PTY_CHARDEV(chr); |
| 341 | |
| 342 | master_fd = qemu_openpty_raw(&slave_fd, &s->pty_name); |
| 343 | if (master_fd < 0) { |
| 344 | error_setg_errno(errp, errno, "Failed to create PTY"); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | close(slave_fd); |
| 349 | if (!qemu_set_blocking(master_fd, false, errp)) { |
| 350 | close(master_fd); |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | qemu_printf("char device redirected to %s (label %s)\n", |
| 355 | s->pty_name, chr->label); |
| 356 | |
| 357 | s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd)); |
| 358 | name = g_strdup_printf("chardev-pty-%s", chr->label); |
| 359 | qio_channel_set_name(s->ioc, name); |
| 360 | g_free(name); |
| 361 | s->timer_src = NULL; |
| 362 | |
| 363 | /* create symbolic link */ |
| 364 | if (path) { |
| 365 | int res = symlink(s->pty_name, path); |
| 366 | |
| 367 | if (res != 0) { |
| 368 | error_setg_errno(errp, errno, "Failed to create PTY symlink"); |
| 369 | return false; |
| 370 | } else { |
| 371 | s->path = g_strdup(path); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | static void pty_chr_parse(QemuOpts *opts, ChardevBackend *backend, Error **errp) |
| 379 | { |
| 380 | const char *path = qemu_opt_get(opts, "path"); |
| 381 | ChardevPty *pty; |
| 382 | |
| 383 | backend->type = CHARDEV_BACKEND_KIND_PTY; |
| 384 | pty = backend->u.pty.data = g_new0(ChardevPty, 1); |
| 385 | qemu_chr_parse_common(opts, qapi_ChardevPty_base(pty)); |
| 386 | pty->path = g_strdup(path); |
| 387 | } |
| 388 | |
| 389 | static char *pty_chr_get_pty_name(Chardev *chr) |
| 390 | { |
| 391 | PtyChardev *s = PTY_CHARDEV(chr); |
| 392 | return g_strdup(s->pty_name); |
| 393 | } |
| 394 | |
| 395 | static char *pty_chr_get_filename(Chardev *chr) |
| 396 | { |
| 397 | PtyChardev *s = PTY_CHARDEV(chr); |
| 398 | return g_strdup_printf("pty:%s", s->pty_name); |
| 399 | } |
| 400 | |
| 401 | static void char_pty_class_init(ObjectClass *oc, const void *data) |
| 402 | { |
| 403 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 404 | |
| 405 | cc->chr_parse = pty_chr_parse; |
| 406 | cc->chr_open = pty_chr_open; |
| 407 | cc->chr_write = pty_chr_write; |
| 408 | cc->chr_update_read_handler = pty_chr_update_read_handler; |
| 409 | cc->chr_add_watch = pty_chr_add_watch; |
| 410 | cc->chr_get_pty_name = pty_chr_get_pty_name; |
| 411 | cc->chr_get_filename = pty_chr_get_filename; |
| 412 | } |
| 413 | |
| 414 | static const TypeInfo char_pty_type_info = { |
| 415 | .name = TYPE_CHARDEV_PTY, |
| 416 | .parent = TYPE_CHARDEV, |
| 417 | .instance_size = sizeof(PtyChardev), |
| 418 | .instance_finalize = char_pty_finalize, |
| 419 | .class_init = char_pty_class_init, |
| 420 | }; |
| 421 | |
| 422 | static void register_types(void) |
| 423 | { |
| 424 | type_register_static(&char_pty_type_info); |
| 425 | } |
| 426 | |
| 427 | type_init(register_types); |