char-pty.c 11 KB

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