]> andersk Git - openssh.git/blob - pty.c
- OpenBSD CVS Changes
[openssh.git] / pty.c
1 /*
2  * 
3  * pty.c
4  * 
5  * Author: Tatu Ylonen <ylo@cs.hut.fi>
6  * 
7  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8  *                    All rights reserved
9  * 
10  * Created: Fri Mar 17 04:37:25 1995 ylo
11  * 
12  * Allocating a pseudo-terminal, and making it the controlling tty.
13  * 
14  */
15
16 #include "includes.h"
17 RCSID("$Id$");
18
19 #include <util.h>
20 #include "pty.h"
21 #include "ssh.h"
22
23 #ifdef HAVE_DEV_PTMX
24 #include <sys/stropts.h>
25 #endif /* HAVE_DEV_PTMX */
26
27 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
28 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
29 #undef HAVE_DEV_PTMX
30 #endif
31
32 #ifdef HAVE_PTY_H
33 # include <pty.h>
34 #endif
35 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
36 # include <sys/stropts.h>
37 #endif
38
39 #ifndef O_NOCTTY
40 #define O_NOCTTY 0
41 #endif
42
43 /*
44  * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
45  * nonzero if a pty was successfully allocated.  On success, open file
46  * descriptors for the pty and tty sides and the name of the tty side are
47  * returned (the buffer must be able to hold at least 64 characters).
48  */
49
50 int 
51 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
52 {
53 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
54         /* openpty(3) exists in OSF/1 and some other os'es */
55         char buf[64];
56         int i;
57
58         i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
59         if (i < 0) {
60                 error("openpty: %.100s", strerror(errno));
61                 return 0;
62         }
63         strlcpy(namebuf, buf, namebuflen);      /* possible truncation */
64         return 1;
65 #else /* HAVE_OPENPTY */
66 #ifdef HAVE__GETPTY
67         /*
68          * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
69          * pty's automagically when needed
70          */
71         char *slave;
72
73         slave = _getpty(ptyfd, O_RDWR, 0622, 0);
74         if (slave == NULL) {
75                 error("_getpty: %.100s", strerror(errno));
76                 return 0;
77         }
78         strlcpy(namebuf, slave, namebuflen);
79         /* Open the slave side. */
80         *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
81         if (*ttyfd < 0) {
82                 error("%.200s: %.100s", namebuf, strerror(errno));
83                 close(*ptyfd);
84                 return 0;
85         }
86         return 1;
87 #else /* HAVE__GETPTY */
88 #ifdef HAVE_DEV_PTMX
89         /*
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.)
92          */
93         int ptm;
94         char *pts;
95
96         ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
97         if (ptm < 0) {
98                 error("/dev/ptmx: %.100s", strerror(errno));
99                 return 0;
100         }
101         if (grantpt(ptm) < 0) {
102                 error("grantpt: %.100s", strerror(errno));
103                 return 0;
104         }
105         if (unlockpt(ptm) < 0) {
106                 error("unlockpt: %.100s", strerror(errno));
107                 return 0;
108         }
109         pts = ptsname(ptm);
110         if (pts == NULL)
111                 error("Slave pty side name could not be obtained.");
112         strlcpy(namebuf, pts, namebuflen);
113         *ptyfd = ptm;
114
115         /* Open the slave side. */
116         *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
117         if (*ttyfd < 0) {
118                 error("%.100s: %.100s", namebuf, strerror(errno));
119                 close(*ptyfd);
120                 return 0;
121         }
122         /* Push the appropriate streams modules, as described in Solaris pts(7). */
123         if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
124                 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
125         if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
126                 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
127         if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
128                 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
129         return 1;
130 #else /* HAVE_DEV_PTMX */
131 #ifdef HAVE_DEV_PTS_AND_PTC
132         /* AIX-style pty code. */
133         const char *name;
134
135         *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
136         if (*ptyfd < 0) {
137                 error("Could not open /dev/ptc: %.100s", strerror(errno));
138                 return 0;
139         }
140         name = ttyname(*ptyfd);
141         if (!name)
142                 fatal("Open of /dev/ptc returns device for which ttyname fails.");
143         strlcpy(namebuf, name, namebuflen);
144         *ttyfd = open(name, O_RDWR | O_NOCTTY);
145         if (*ttyfd < 0) {
146                 error("Could not open pty slave side %.100s: %.100s",
147                       name, strerror(errno));
148                 close(*ptyfd);
149                 return 0;
150         }
151         return 1;
152 #else /* HAVE_DEV_PTS_AND_PTC */
153         /* BSD-style pty code. */
154         char buf[64];
155         int i;
156         const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
157         const char *ptyminors = "0123456789abcdef";
158         int num_minors = strlen(ptyminors);
159         int num_ptys = strlen(ptymajors) * num_minors;
160
161         for (i = 0; i < num_ptys; i++) {
162                 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
163                          ptyminors[i % num_minors]);
164                 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
165                 if (*ptyfd < 0)
166                         continue;
167                 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
168                     ptymajors[i / num_minors], ptyminors[i % num_minors]);
169
170                 /* Open the slave side. */
171                 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
172                 if (*ttyfd < 0) {
173                         error("%.100s: %.100s", namebuf, strerror(errno));
174                         close(*ptyfd);
175                         return 0;
176                 }
177                 return 1;
178         }
179         return 0;
180 #endif /* HAVE_DEV_PTS_AND_PTC */
181 #endif /* HAVE_DEV_PTMX */
182 #endif /* HAVE__GETPTY */
183 #endif /* HAVE_OPENPTY */
184 }
185
186 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
187
188 void 
189 pty_release(const char *ttyname)
190 {
191         if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
192                 debug("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
193         if (chmod(ttyname, (mode_t) 0666) < 0)
194                 debug("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
195 }
196
197 /* Makes the tty the processes controlling tty and sets it to sane modes. */
198
199 void 
200 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
201 {
202         int fd;
203
204         /* First disconnect from the old controlling tty. */
205 #ifdef TIOCNOTTY
206         fd = open("/dev/tty", O_RDWR | O_NOCTTY);
207         if (fd >= 0) {
208                 (void) ioctl(fd, TIOCNOTTY, NULL);
209                 close(fd);
210         }
211 #endif /* TIOCNOTTY */
212         if (setsid() < 0)
213                 error("setsid: %.100s", strerror(errno));
214
215         /*
216          * Verify that we are successfully disconnected from the controlling
217          * tty.
218          */
219         fd = open("/dev/tty", O_RDWR | O_NOCTTY);
220         if (fd >= 0) {
221                 error("Failed to disconnect from controlling tty.");
222                 close(fd);
223         }
224         /* Make it our controlling tty. */
225 #ifdef TIOCSCTTY
226         debug("Setting controlling tty using TIOCSCTTY.");
227         /*
228          * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
229          * returns EINVAL with these arguments, and there is absolutely no
230          * documentation.
231          */
232         ioctl(*ttyfd, TIOCSCTTY, NULL);
233 #endif /* TIOCSCTTY */
234         fd = open(ttyname, O_RDWR);
235         if (fd < 0)
236                 error("%.100s: %.100s", ttyname, strerror(errno));
237         else
238                 close(fd);
239
240         /* Verify that we now have a controlling tty. */
241         fd = open("/dev/tty", O_WRONLY);
242         if (fd < 0)
243                 error("open /dev/tty failed - could not set controlling tty: %.100s",
244                       strerror(errno));
245         else {
246                 close(fd);
247         }
248 }
249
250 /* Changes the window size associated with the pty. */
251
252 void 
253 pty_change_window_size(int ptyfd, int row, int col,
254                        int xpixel, int ypixel)
255 {
256         struct winsize w;
257         w.ws_row = row;
258         w.ws_col = col;
259         w.ws_xpixel = xpixel;
260         w.ws_ypixel = ypixel;
261         (void) ioctl(ptyfd, TIOCSWINSZ, &w);
262 }
This page took 1.718016 seconds and 5 git commands to generate.