Start converting to GPL v3+ (ref: ticket #23900)
[screen-lua.git] / src / pty.c
bloba98322217ed6269bfbbcfc850e91c392d814ef82
1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, see
18 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 ****************************************************************
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <signal.h>
29 #include "config.h"
30 #include "screen.h"
32 #ifndef sun
33 # include <sys/ioctl.h>
34 #endif
36 /* for solaris 2.1, Unixware (SVR4.2) and possibly others */
37 #ifdef HAVE_STROPTS_H
38 # include <sys/stropts.h>
39 #endif
41 #if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
42 # include <sys/ttold.h>
43 #endif
45 #ifdef ISC
46 # include <sys/tty.h>
47 # include <sys/sioctl.h>
48 # include <sys/pty.h>
49 #endif
51 #ifdef sgi
52 # include <sys/sysmacros.h>
53 #endif /* sgi */
55 #include "extern.h"
58 * if no PTYRANGE[01] is in the config file, we pick a default
60 #ifndef PTYRANGE0
61 # define PTYRANGE0 "qpr"
62 #endif
63 #ifndef PTYRANGE1
64 # define PTYRANGE1 "0123456789abcdef"
65 #endif
67 /* SVR4 pseudo ttys don't seem to work with SCO-5 */
68 #ifdef M_UNIX
69 # undef HAVE_SVR4_PTYS
70 #endif
72 extern int eff_uid;
74 /* used for opening a new pty-pair: */
75 static char PtyName[32], TtyName[32];
77 #if !(defined(sequent) || defined(_SEQUENT_) || defined(HAVE_SVR4_PTYS))
78 # ifdef hpux
79 static char PtyProto[] = "/dev/ptym/ptyXY";
80 static char TtyProto[] = "/dev/pty/ttyXY";
81 # else
82 # ifdef M_UNIX
83 static char PtyProto[] = "/dev/ptypXY";
84 static char TtyProto[] = "/dev/ttypXY";
85 # else
86 static char PtyProto[] = "/dev/ptyXY";
87 static char TtyProto[] = "/dev/ttyXY";
88 # endif
89 # endif /* hpux */
90 #endif
92 static void initmaster __P((int));
94 #if defined(sun)
95 /* sun's utmp_update program opens the salve side, thus corrupting
97 int pty_preopen = 1;
98 #else
99 int pty_preopen = 0;
100 #endif
103 * Open all ptys with O_NOCTTY, just to be on the safe side
104 * (RISCos mips breaks otherwise)
106 #ifndef O_NOCTTY
107 # define O_NOCTTY 0
108 #endif
110 /***************************************************************/
112 static void
113 initmaster(f)
114 int f;
116 #ifdef POSIX
117 tcflush(f, TCIOFLUSH);
118 #else
119 # ifdef TIOCFLUSH
120 (void) ioctl(f, TIOCFLUSH, (char *) 0);
121 # endif
122 #endif
123 #ifdef LOCKPTY
124 (void) ioctl(f, TIOCEXCL, (char *) 0);
125 #endif
128 void
129 InitPTY(f)
130 int f;
132 if (f < 0)
133 return;
134 #if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
135 if (ioctl(f, I_PUSH, "ptem"))
136 Panic(errno, "InitPTY: cannot I_PUSH ptem");
137 if (ioctl(f, I_PUSH, "ldterm"))
138 Panic(errno, "InitPTY: cannot I_PUSH ldterm");
139 # ifdef sun
140 if (ioctl(f, I_PUSH, "ttcompat"))
141 Panic(errno, "InitPTY: cannot I_PUSH ttcompat");
142 # endif
143 #endif
146 /***************************************************************/
148 #if defined(OSX) && !defined(PTY_DONE)
149 #define PTY_DONE
151 OpenPTY(ttyn)
152 char **ttyn;
154 register int f;
155 if ((f = open_controlling_pty(TtyName)) < 0)
156 return -1;
157 initmaster(f);
158 *ttyn = TtyName;
159 return f;
161 #endif
163 /***************************************************************/
165 #if (defined(sequent) || defined(_SEQUENT_)) && !defined(PTY_DONE)
166 #define PTY_DONE
168 OpenPTY(ttyn)
169 char **ttyn;
171 char *m, *s;
172 register int f;
174 if ((f = getpseudotty(&s, &m)) < 0)
175 return -1;
176 #ifdef _SEQUENT_
177 fvhangup(s);
178 #endif
179 strncpy(PtyName, m, sizeof(PtyName));
180 strncpy(TtyName, s, sizeof(TtyName));
181 initmaster(f);
182 *ttyn = TtyName;
183 return f;
185 #endif
187 /***************************************************************/
189 #if defined(__sgi) && !defined(PTY_DONE)
190 #define PTY_DONE
192 OpenPTY(ttyn)
193 char **ttyn;
195 int f;
196 char *name, *_getpty();
197 sigret_t (*sigcld)__P(SIGPROTOARG);
200 * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
201 * exec() /usr/adm/mkpts
203 sigcld = signal(SIGCHLD, SIG_DFL);
204 name = _getpty(&f, O_RDWR | O_NONBLOCK, 0600, 0);
205 signal(SIGCHLD, sigcld);
207 if (name == 0)
208 return -1;
209 initmaster(f);
210 *ttyn = name;
211 return f;
213 #endif
215 /***************************************************************/
217 #if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
218 #define PTY_DONE
220 OpenPTY(ttyn)
221 char **ttyn;
223 register int f;
224 struct stat buf;
226 strcpy(PtyName, "/dev/ptc");
227 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
228 return -1;
229 if (fstat(f, &buf) < 0)
231 close(f);
232 return -1;
234 sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
235 initmaster(f);
236 *ttyn = TtyName;
237 return f;
239 #endif
241 /***************************************************************/
243 #if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE)
244 #define PTY_DONE
246 OpenPTY(ttyn)
247 char **ttyn;
249 register int f;
250 char *m, *ptsname();
251 int unlockpt __P((int)), grantpt __P((int));
252 #if defined(HAVE_GETPT) && defined(linux)
253 int getpt __P((void));
254 #endif
255 sigret_t (*sigcld)__P(SIGPROTOARG);
257 strcpy(PtyName, "/dev/ptmx");
258 #if defined(HAVE_GETPT) && defined(linux)
259 if ((f = getpt()) == -1)
260 #else
261 if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
262 #endif
263 return -1;
266 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
267 * exec()s pt_chmod
269 sigcld = signal(SIGCHLD, SIG_DFL);
270 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
272 signal(SIGCHLD, sigcld);
273 close(f);
274 return -1;
276 signal(SIGCHLD, sigcld);
277 strncpy(TtyName, m, sizeof(TtyName));
278 initmaster(f);
279 *ttyn = TtyName;
280 return f;
282 #endif
284 /***************************************************************/
286 #if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
287 #define PTY_DONE
290 OpenPTY(ttyn)
291 char **ttyn;
293 register int f;
295 /* a dumb looking loop replaced by mycrofts code: */
296 strcpy (PtyName, "/dev/ptc");
297 if ((f = open (PtyName, O_RDWR | O_NOCTTY)) < 0)
298 return -1;
299 strncpy(TtyName, ttyname(f), sizeof(TtyName));
300 if (eff_uid && access(TtyName, R_OK | W_OK))
302 close(f);
303 return -1;
305 initmaster(f);
306 # ifdef _IBMR2
307 pty_preopen = 1;
308 # endif
309 *ttyn = TtyName;
310 return f;
312 #endif
314 /***************************************************************/
316 #if defined(HAVE_OPENPTY) && !defined(PTY_DONE)
317 #define PTY_DONE
319 OpenPTY(ttyn)
320 char **ttyn;
322 int f, s;
323 if (openpty(&f, &s, TtyName, NULL, NULL) != 0)
324 return -1;
325 close(s);
326 initmaster(f);
327 pty_preopen = 1;
328 *ttyn = TtyName;
329 return f;
331 #endif
333 /***************************************************************/
335 #ifndef PTY_DONE
337 OpenPTY(ttyn)
338 char **ttyn;
340 register char *p, *q, *l, *d;
341 register int f;
343 debug("OpenPTY: Using BSD style ptys.\n");
344 strcpy(PtyName, PtyProto);
345 strcpy(TtyName, TtyProto);
346 for (p = PtyName; *p != 'X'; p++)
348 for (q = TtyName; *q != 'X'; q++)
350 for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
352 for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
354 debug1("OpenPTY tries '%s'\n", PtyName);
355 if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
356 continue;
357 q[0] = *l;
358 q[1] = *d;
359 if (eff_uid && access(TtyName, R_OK | W_OK))
361 close(f);
362 continue;
364 #if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
365 /* Hack to ensure that the slave side of the pty is
366 * unused. May not work in anything other than SunOS4.1
369 int pgrp;
371 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
372 if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
374 close(f);
375 continue;
378 #endif
379 initmaster(f);
380 *ttyn = TtyName;
381 return f;
384 return -1;
386 #endif