xref: /vim-8.2.3635/src/pty.c (revision cf2d8dee)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved		by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 /*
10  * The stuff in this file mostly comes from the "screen" program.
11  * Included with permission from Juergen Weigert.
12  * Copied from "pty.c".  "putenv.c" was used for putenv() in misc2.c.
13  *
14  * It has been modified to work better with Vim.
15  * The parts that are not used in Vim have been deleted.
16  * See the "screen" sources for the complete stuff.
17  *
18  * This specific version is distibuted under the Vim license (attribution by
19  * Juergen Weigert), the GPL applies to the original version, see the
20  * copyright notice below.
21  */
22 
23 /* Copyright (c) 1993
24  *	Juergen Weigert ([email protected])
25  *	Michael Schroeder ([email protected])
26  * Copyright (c) 1987 Oliver Laumann
27  *
28  * This program is free software; you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation; either version 2, or (at your option)
31  * any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program (see the file COPYING); if not, write to the
40  * Free Software Foundation, Inc.,
41  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
42  */
43 
44 #include "vim.h"
45 
46 #include <signal.h>
47 
48 #ifdef __CYGWIN32__
49 # include <sys/termios.h>
50 #endif
51 
52 #ifdef HAVE_SYS_IOCTL_H
53 # include <sys/ioctl.h>
54 #endif
55 
56 #if HAVE_STROPTS_H
57 #include <sys/types.h>
58 #ifdef sinix
59 #define buf_T __system_buf_t__
60 #endif
61 #include <stropts.h>
62 #ifdef sinix
63 #undef buf_T
64 #endif
65 # ifdef sun
66 #  include <sys/conf.h>
67 # endif
68 #endif
69 
70 #ifdef HAVE_UNISTD_H
71 # include <unistd.h>
72 #endif
73 
74 #if HAVE_TERMIO_H
75 # include <termio.h>
76 #else
77 # ifdef HAVE_TERMIOS_H
78 #  include <termios.h>
79 # endif
80 #endif
81 
82 #if HAVE_SYS_STREAM_H
83 # include <sys/stream.h>
84 #endif
85 
86 #if HAVE_SYS_PTEM_H
87 # include <sys/ptem.h>
88 #endif
89 
90 #if !defined(sun) && !defined(VMS) && !defined(MACOS)
91 # include <sys/ioctl.h>
92 #endif
93 
94 #if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
95 # include <sys/ttold.h>
96 #endif
97 
98 #ifdef ISC
99 # include <sys/tty.h>
100 # include <sys/sioctl.h>
101 # include <sys/pty.h>
102 #endif
103 
104 #ifdef sgi
105 # include <sys/sysmacros.h>
106 #endif
107 
108 #if defined(_INCLUDE_HPUX_SOURCE) && !defined(hpux)
109 # define hpux
110 #endif
111 
112 /*
113  * if no PTYRANGE[01] is in the config file, we pick a default
114  */
115 #ifndef PTYRANGE0
116 # define PTYRANGE0 "qprs"
117 #endif
118 #ifndef PTYRANGE1
119 # define PTYRANGE1 "0123456789abcdef"
120 #endif
121 
122 /* SVR4 pseudo ttys don't seem to work with SCO-5 */
123 #ifdef M_UNIX
124 # undef HAVE_SVR4_PTYS
125 #endif
126 
127 static void initmaster(int);
128 
129 /*
130  *  Open all ptys with O_NOCTTY, just to be on the safe side.
131  */
132 #ifndef O_NOCTTY
133 # define O_NOCTTY 0
134 #endif
135 
136     static void
137 initmaster(int f UNUSED)
138 {
139 #ifndef VMS
140 # ifdef POSIX
141     tcflush(f, TCIOFLUSH);
142 # else
143 #  ifdef TIOCFLUSH
144     (void)ioctl(f, TIOCFLUSH, (char *) 0);
145 #  endif
146 # endif
147 # ifdef LOCKPTY
148     (void)ioctl(f, TIOCEXCL, (char *) 0);
149 # endif
150 #endif
151 }
152 
153 /*
154  * This causes a hang on some systems, but is required for a properly working
155  * pty on others.  Needs to be tuned...
156  */
157     int
158 SetupSlavePTY(int fd)
159 {
160     if (fd < 0)
161 	return 0;
162 #if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
163 # if defined(HAVE_SYS_PTEM_H) || defined(hpux)
164     if (ioctl(fd, I_PUSH, "ptem") != 0)
165 	return -1;
166 # endif
167     if (ioctl(fd, I_PUSH, "ldterm") != 0)
168 	return -1;
169 # ifdef sun
170     if (ioctl(fd, I_PUSH, "ttcompat") != 0)
171 	return -1;
172 # endif
173 #endif
174     return 0;
175 }
176 
177 
178 #if defined(OSX) && !defined(PTY_DONE)
179 #define PTY_DONE
180     int
181 OpenPTY(char **ttyn)
182 {
183     int		f;
184     static char TtyName[32];
185 
186     if ((f = open_controlling_pty(TtyName)) < 0)
187 	return -1;
188     initmaster(f);
189     *ttyn = TtyName;
190     return f;
191 }
192 #endif
193 
194 #if (defined(sequent) || defined(_SEQUENT_)) && defined(HAVE_GETPSEUDOTTY) \
195 	&& !defined(PTY_DONE)
196 #define PTY_DONE
197     int
198 OpenPTY(char **ttyn)
199 {
200     char	*m, *s;
201     int		f;
202     /* used for opening a new pty-pair: */
203     static char PtyName[32];
204     static char TtyName[32];
205 
206     if ((f = getpseudotty(&s, &m)) < 0)
207 	return -1;
208 #ifdef _SEQUENT_
209     fvhangup(s);
210 #endif
211     vim_strncpy((char_u *)PtyName, (char_u *)m, sizeof(PtyName) - 1);
212     vim_strncpy((char_u *)TtyName, (char_u *)s, sizeof(TtyName) - 1);
213     initmaster(f);
214     *ttyn = TtyName;
215     return f;
216 }
217 #endif
218 
219 #if defined(__sgi) && !defined(PTY_DONE)
220 #define PTY_DONE
221     int
222 OpenPTY(char **ttyn)
223 {
224     int f;
225     char *name;
226     RETSIGTYPE (*sigcld) SIGPROTOARG;
227 
228     /*
229      * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
230      * exec() /usr/adm/mkpts
231      */
232     sigcld = signal(SIGCHLD, SIG_DFL);
233     name = _getpty(&f, O_RDWR | O_NONBLOCK | O_EXTRA, 0600, 0);
234     signal(SIGCHLD, sigcld);
235 
236     if (name == 0)
237 	return -1;
238     initmaster(f);
239     *ttyn = name;
240     return f;
241 }
242 #endif
243 
244 #if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
245 #define PTY_DONE
246     int
247 OpenPTY(char **ttyn)
248 {
249     int		f;
250     struct stat buf;
251     /* used for opening a new pty-pair: */
252     static char TtyName[32];
253 
254     if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_NONBLOCK | O_EXTRA, 0)) < 0)
255 	return -1;
256     if (mch_fstat(f, &buf) < 0)
257     {
258 	close(f);
259 	return -1;
260     }
261     sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
262     initmaster(f);
263     *ttyn = TtyName;
264     return f;
265 }
266 #endif
267 
268 #if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE) && !defined(hpux) && !defined(MACOS_X)
269 
270 /* NOTE: Even though HPUX can have /dev/ptmx, the code below doesn't work!
271  * Same for Mac OS X Leopard. */
272 #define PTY_DONE
273     int
274 OpenPTY(char **ttyn)
275 {
276     int		f;
277     char	*m;
278     char	*(ptsname(int));
279     int		unlockpt(int);
280     int		grantpt(int);
281     RETSIGTYPE (*sigcld) SIGPROTOARG;
282     /* used for opening a new pty-pair: */
283     static char TtyName[32];
284 
285     if ((f = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
286 	return -1;
287 
288     /*
289      * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
290      * exec()s pt_chmod
291      */
292     sigcld = signal(SIGCHLD, SIG_DFL);
293     if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
294     {
295 	signal(SIGCHLD, sigcld);
296 	close(f);
297 	return -1;
298     }
299     signal(SIGCHLD, sigcld);
300     vim_strncpy((char_u *)TtyName, (char_u *)m, sizeof(TtyName) - 1);
301     initmaster(f);
302     *ttyn = TtyName;
303     return f;
304 }
305 #endif
306 
307 #if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
308 #define PTY_DONE
309 
310 #ifdef _IBMR2
311 int aixhack = -1;
312 #endif
313 
314     int
315 OpenPTY(char **ttyn)
316 {
317     int		f;
318     /* used for opening a new pty-pair: */
319     static char TtyName[32];
320 
321     /* a dumb looking loop replaced by mycrofts code: */
322     if ((f = open("/dev/ptc", O_RDWR | O_NOCTTY | O_EXTRA)) < 0)
323 	return -1;
324     vim_strncpy((char_u *)TtyName, (char_u *)ttyname(f), sizeof(TtyName) - 1);
325     if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
326     {
327 	close(f);
328 	return -1;
329     }
330     initmaster(f);
331 # ifdef _IBMR2
332     if (aixhack >= 0)
333 	close(aixhack);
334     if ((aixhack = open(TtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) < 0)
335     {
336 	close(f);
337 	return -1;
338     }
339 # endif
340     *ttyn = TtyName;
341     return f;
342 }
343 #endif
344 
345 #ifndef PTY_DONE
346 
347 # ifdef hpux
348 static char PtyProto[] = "/dev/ptym/ptyXY";
349 static char TtyProto[] = "/dev/pty/ttyXY";
350 # else
351 #  ifdef __BEOS__
352 static char PtyProto[] = "/dev/pt/XY";
353 static char TtyProto[] = "/dev/tt/XY";
354 #  else
355 static char PtyProto[] = "/dev/ptyXY";
356 static char TtyProto[] = "/dev/ttyXY";
357 #  endif
358 # endif
359 
360     int
361 OpenPTY(char **ttyn)
362 {
363     char	*p, *q, *l, *d;
364     int		f;
365     /* used for opening a new pty-pair: */
366     static char PtyName[32];
367     static char TtyName[32];
368 
369     strcpy(PtyName, PtyProto);
370     strcpy(TtyName, TtyProto);
371     for (p = PtyName; *p != 'X'; p++)
372 	;
373     for (q = TtyName; *q != 'X'; q++)
374 	;
375     for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
376     {
377 	for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
378 	{
379 #if !defined(MACOS) || defined(USE_CARBONIZED)
380 	    if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA, 0)) == -1)
381 #else
382 	    if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_EXTRA)) == -1)
383 #endif
384 		continue;
385 	    q[0] = *l;
386 	    q[1] = *d;
387 #ifndef MACOS
388 	    if (geteuid() != ROOT_UID && mch_access(TtyName, R_OK | W_OK))
389 	    {
390 		close(f);
391 		continue;
392 	    }
393 #endif
394 #if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
395 	    /* Hack to ensure that the slave side of the pty is
396 	     * unused. May not work in anything other than SunOS4.1
397 	     */
398 	    {
399 		int pgrp;
400 
401 		/* tcgetpgrp does not work (uses TIOCGETPGRP)! */
402 		if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
403 		{
404 		    close(f);
405 		    continue;
406 		}
407 	    }
408 #endif
409 	    initmaster(f);
410 	    *ttyn = TtyName;
411 	    return f;
412 	}
413     }
414     return -1;
415 }
416 #endif
417