tcg-arm: Rename use_armv5_instructions to use_armvt5_instructions
[qemu/kevin.git] / util / qemu-openpty.c
blob4febfe9d724503c67eb5489edd9a559fc4f1e397
1 /*
2 * qemu-openpty.c
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010 Red Hat, Inc.
7 * Wrapper function qemu_openpty() implementation.
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
29 * This is not part of oslib-posix.c because this function
30 * uses openpty() which often in -lutil, and if we add this
31 * dependency to oslib-posix.o, every app will have to be
32 * linked with -lutil.
35 #include "config-host.h"
36 #include "qemu-common.h"
38 #if defined(__GLIBC__)
39 # include <pty.h>
40 #elif defined CONFIG_BSD
41 # include <termios.h>
42 # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
43 # include <libutil.h>
44 # else
45 # include <util.h>
46 # endif
47 #elif defined CONFIG_SOLARIS
48 # include <termios.h>
49 # include <stropts.h>
50 #endif
52 #ifdef __sun__
53 /* Once Solaris has openpty(), this is going to be removed. */
54 static int openpty(int *amaster, int *aslave, char *name,
55 struct termios *termp, struct winsize *winp)
57 const char *slave;
58 int mfd = -1, sfd = -1;
60 *amaster = *aslave = -1;
62 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
63 if (mfd < 0)
64 goto err;
66 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
67 goto err;
69 if ((slave = ptsname(mfd)) == NULL)
70 goto err;
72 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
73 goto err;
75 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
76 (termp != NULL && tcgetattr(sfd, termp) < 0))
77 goto err;
79 if (amaster)
80 *amaster = mfd;
81 if (aslave)
82 *aslave = sfd;
83 if (winp)
84 ioctl(sfd, TIOCSWINSZ, winp);
86 return 0;
88 err:
89 if (sfd != -1)
90 close(sfd);
91 close(mfd);
92 return -1;
95 static void cfmakeraw (struct termios *termios_p)
97 termios_p->c_iflag &=
98 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
99 termios_p->c_oflag &= ~OPOST;
100 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
101 termios_p->c_cflag &= ~(CSIZE|PARENB);
102 termios_p->c_cflag |= CS8;
104 termios_p->c_cc[VMIN] = 0;
105 termios_p->c_cc[VTIME] = 0;
107 #endif
109 int qemu_openpty_raw(int *aslave, char *pty_name)
111 int amaster;
112 struct termios tty;
113 #if defined(__OpenBSD__) || defined(__DragonFly__)
114 char pty_buf[PATH_MAX];
115 #define q_ptsname(x) pty_buf
116 #else
117 char *pty_buf = NULL;
118 #define q_ptsname(x) ptsname(x)
119 #endif
121 if (openpty(&amaster, aslave, pty_buf, NULL, NULL) < 0) {
122 return -1;
125 /* Set raw attributes on the pty. */
126 tcgetattr(*aslave, &tty);
127 cfmakeraw(&tty);
128 tcsetattr(*aslave, TCSAFLUSH, &tty);
130 if (pty_name) {
131 strcpy(pty_name, q_ptsname(amaster));
134 return amaster;