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>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 # include <sys/ioctl.h>
55 #endif /* HAVE_UTIL_H */
60 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
61 # include <sys/stropts.h>
73 openpty(int *amaster, int *aslave, char *name, struct termios *termp,
76 #if defined(HAVE__GETPTY)
78 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
79 * pty's automagically when needed
83 if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL)
86 /* Open the slave side. */
87 if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) {
93 #elif defined(HAVE_DEV_PTMX)
95 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
96 * also has bsd-style ptys, but they simply do not work.)
102 if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
105 /* XXX: need to close ptm on error? */
106 old_signal = signal(SIGCHLD, SIG_DFL);
107 if (grantpt(ptm) < 0)
109 signal(SIGCHLD, old_signal);
111 if (unlockpt(ptm) < 0)
114 if ((pts = ptsname(ptm)) == NULL)
118 /* Open the slave side. */
119 if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) {
125 * Try to push the appropriate streams modules, as described
128 ioctl(*aslave, I_PUSH, "ptem");
129 ioctl(*aslave, I_PUSH, "ldterm");
131 ioctl(*aslave, I_PUSH, "ttcompat");
136 #elif defined(HAVE_DEV_PTS_AND_PTC)
137 /* AIX-style pty code. */
140 if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1)
142 if ((ttname = ttyname(*amaster)) == NULL)
144 if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) {
150 #elif defined(_UNICOS)
151 char ptbuf[64], ttbuf[64];
157 if ((highpty = sysconf(_SC_CRAY_NPTY)) == -1)
159 #endif /* _SC_CRAY_NPTY */
161 for (i = 0; i < highpty; i++) {
162 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty/%03d", i);
163 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%03d", i);
164 if ((*amaster = open(ptbuf, O_RDWR|O_NOCTTY)) == -1)
166 /* Open the slave side. */
167 if ((*aslave = open(ttbuf, O_RDWR|O_NOCTTY)) == -1) {
176 /* BSD-style pty code. */
177 char ptbuf[64], ttbuf[64];
179 const char *ptymajors = "pqrstuvwxyzabcdefghijklmno"
180 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
181 const char *ptyminors = "0123456789abcdef";
182 int num_minors = strlen(ptyminors);
183 int num_ptys = strlen(ptymajors) * num_minors;
186 for (i = 0; i < num_ptys; i++) {
187 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c",
188 ptymajors[i / num_minors], ptyminors[i % num_minors]);
189 snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c",
190 ptymajors[i / num_minors], ptyminors[i % num_minors]);
192 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) {
193 /* Try SCO style naming */
194 snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i);
195 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i);
196 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1)
200 /* Open the slave side. */
201 if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) {
205 /* set tty modes to a sane state for broken clients */
206 if (tcgetattr(*amaster, &tio) != -1) {
207 tio.c_lflag |= (ECHO | ISIG | ICANON);
208 tio.c_oflag |= (OPOST | ONLCR);
209 tio.c_iflag |= ICRNL;
210 tcsetattr(*amaster, TCSANOW, &tio);
219 #endif /* !defined(HAVE_OPENPTY) */