2 * Please note: this implementation of openpty() is far from complete.
3 * it is just enough for portable OpenSSH's needs.
7 * Copyright (c) 2004 Damien Miller <djm@mindrot.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: Tatu Ylonen <ylo@cs.hut.fi>
24 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
26 * Allocating a pseudo-terminal, and making it the controlling tty.
28 * As far as I am concerned, the code I have written for this software
29 * can be used freely for any purpose. Any derived versions of this
30 * software must be clearly marked as such, and if the derived work is
31 * incompatible with the protocol description in the RFC file, it must be
32 * called by a name other than "ssh" or "Secure Shell".
36 #if !defined(HAVE_OPENPTY)
38 #include <sys/types.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
50 #endif /* HAVE_UTIL_H */
55 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
56 # include <sys/stropts.h>
68 openpty(int *amaster, int *aslave, char *name, struct termios *termp,
71 #if defined(HAVE__GETPTY)
73 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
74 * pty's automagically when needed
78 if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL)
81 /* Open the slave side. */
82 if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) {
88 #elif defined(HAVE_DEV_PTMX)
90 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
91 * also has bsd-style ptys, but they simply do not work.)
97 if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
100 /* XXX: need to close ptm on error? */
101 old_signal = signal(SIGCHLD, SIG_DFL);
102 if (grantpt(ptm) < 0)
104 signal(SIGCHLD, old_signal);
106 if (unlockpt(ptm) < 0)
109 if ((pts = ptsname(ptm)) == NULL)
113 /* Open the slave side. */
114 if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) {
120 * Try to push the appropriate streams modules, as described
123 ioctl(*aslave, I_PUSH, "ptem");
124 ioctl(*aslave, I_PUSH, "ldterm");
126 ioctl(*aslave, I_PUSH, "ttcompat");
131 #elif defined(HAVE_DEV_PTS_AND_PTC)
132 /* AIX-style pty code. */
135 if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1)
137 if ((ttname = ttyname(*amaster)) == NULL)
139 if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) {
145 #elif defined(_UNICOS)
146 char ptbuf[64], ttbuf[64];
152 if ((highpty = sysconf(_SC_CRAY_NPTY)) == -1)
154 #endif /* _SC_CRAY_NPTY */
156 for (i = 0; i < highpty; i++) {
157 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty/%03d", i);
158 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%03d", i);
159 if ((*amaster = open(ptbuf, O_RDWR|O_NOCTTY)) == -1)
161 /* Open the slave side. */
162 if ((*aslave = open(ttbuf, O_RDWR|O_NOCTTY)) == -1) {
171 /* BSD-style pty code. */
172 char ptbuf[64], ttbuf[64];
174 const char *ptymajors = "pqrstuvwxyzabcdefghijklmno"
175 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
176 const char *ptyminors = "0123456789abcdef";
177 int num_minors = strlen(ptyminors);
178 int num_ptys = strlen(ptymajors) * num_minors;
181 for (i = 0; i < num_ptys; i++) {
182 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c",
183 ptymajors[i / num_minors], ptyminors[i % num_minors]);
184 snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c",
185 ptymajors[i / num_minors], ptyminors[i % num_minors]);
187 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) {
188 /* Try SCO style naming */
189 snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i);
190 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i);
191 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1)
195 /* Open the slave side. */
196 if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) {
200 /* set tty modes to a sane state for broken clients */
201 if (tcgetattr(*amaster, &tio) != -1) {
202 tio.c_lflag |= (ECHO | ISIG | ICANON);
203 tio.c_oflag |= (OPOST | ONLCR);
204 tio.c_iflag |= ICRNL;
205 tcsetattr(*amaster, TCSANOW, &tio);
214 #endif /* !defined(HAVE_OPENPTY) */