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. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Author: Tatu Ylonen <ylo@cs.hut.fi>
32 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
34 * Allocating a pseudo-terminal, and making it the controlling tty.
36 * As far as I am concerned, the code I have written for this software
37 * can be used freely for any purpose. Any derived versions of this
38 * software must be clearly marked as such, and if the derived work is
39 * incompatible with the protocol description in the RFC file, it must be
40 * called by a name other than "ssh" or "Secure Shell".
44 #if !defined(HAVE_OPENPTY)
48 #endif /* HAVE_UTIL_H */
53 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
54 # include <sys/stropts.h>
62 openpty(int *amaster, int *aslave, char *name, struct termios *termp,
65 #if defined(HAVE__GETPTY)
67 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
68 * pty's automagically when needed
72 if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL)
75 /* Open the slave side. */
76 if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) {
82 #elif defined(HAVE_DEV_PTMX)
84 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
85 * also has bsd-style ptys, but they simply do not work.)
91 if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
94 /* XXX: need to close ptm on error? */
95 old_signal = signal(SIGCHLD, SIG_DFL);
98 signal(SIGCHLD, old_signal);
100 if (unlockpt(ptm) < 0)
103 if ((pts = ptsname(ptm)) == NULL)
107 /* Open the slave side. */
108 if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) {
115 * Try to push the appropriate streams modules, as described
118 ioctl(*aslave, I_PUSH, "ptem");
119 ioctl(*aslave, I_PUSH, "ldterm");
121 ioctl(*aslave, I_PUSH, "ttcompat");
123 #endif /* HAVE_CYGWIN */
127 #elif defined(HAVE_DEV_PTS_AND_PTC)
128 /* AIX-style pty code. */
131 if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1)
133 if ((ttname = ttyname(*amaster)) == NULL)
135 if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) {
141 #elif defined(_UNICOS)
142 char ptbuf[64], ttbuf[64];
148 if ((highpty = sysconf(_SC_CRAY_NPTY)) == -1)
150 #endif /* _SC_CRAY_NPTY */
152 for (i = 0; i < highpty; i++) {
153 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty/%03d", i);
154 snprintf(ttbuf, sideof(ttbuf), "/dev/ttyp%03d", i);
155 if ((*amaster = open(ptbuf, O_RDWR|O_NOCTTY)) == -1)
157 /* Open the slave side. */
158 if ((*aslave = open(ttbuf, O_RDWR|O_NOCTTY)) == -1) {
167 /* BSD-style pty code. */
168 char ptbuf[64], ttbuf[64];
170 const char *ptymajors = "pqrstuvwxyzabcdefghijklmno"
171 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
172 const char *ptyminors = "0123456789abcdef";
173 int num_minors = strlen(ptyminors);
174 int num_ptys = strlen(ptymajors) * num_minors;
177 for (i = 0; i < num_ptys; i++) {
178 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c",
179 ptymajors[i / num_minors], ptyminors[i % num_minors]);
180 snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c",
181 ptymajors[i / num_minors], ptyminors[i % num_minors]);
183 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) {
184 /* Try SCO style naming */
185 snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i);
186 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i);
187 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1)
191 /* Open the slave side. */
192 if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) {
196 /* set tty modes to a sane state for broken clients */
197 if (tcgetattr(*amaster, &tio) != -1) {
198 tio.c_lflag |= (ECHO | ISIG | ICANON);
199 tio.c_oflag |= (OPOST | ONLCR);
200 tio.c_iflag |= ICRNL;
201 tcsetattr(*amaster, TCSANOW, &tio);
210 #endif /* !defined(HAVE_OPENPTY) */