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 2, or (at your option)
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, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 ****************************************************************
24 #include <sys/types.h>
33 # include <sys/ioctl.h>
36 /* for solaris 2.1, Unixware (SVR4.2) and possibly others */
38 # include <sys/stropts.h>
41 #if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
42 # include <sys/ttold.h>
47 # include <sys/sioctl.h>
52 # include <sys/sysmacros.h>
58 * if no PTYRANGE[01] is in the config file, we pick a default
61 # define PTYRANGE0 "qpr"
64 # define PTYRANGE1 "0123456789abcdef"
67 /* SVR4 pseudo ttys don't seem to work with SCO-5 */
69 # undef HAVE_SVR4_PTYS
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))
79 static char PtyProto[] = "/dev/ptym/ptyXY";
80 static char TtyProto[] = "/dev/pty/ttyXY";
83 static char PtyProto[] = "/dev/ptypXY";
84 static char TtyProto[] = "/dev/ttypXY";
86 static char PtyProto[] = "/dev/ptyXY";
87 static char TtyProto[] = "/dev/ttyXY";
92 static void initmaster __P((int));
95 /* sun's utmp_update program opens the salve side, thus corrupting
103 * Open all ptys with O_NOCTTY, just to be on the safe side
104 * (RISCos mips breaks otherwise)
110 /***************************************************************/
117 tcflush(f, TCIOFLUSH);
120 (void) ioctl(f, TIOCFLUSH, (char *) 0);
124 (void) ioctl(f, TIOCEXCL, (char *) 0);
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");
140 if (ioctl(f, I_PUSH, "ttcompat"))
141 Panic(errno, "InitPTY: cannot I_PUSH ttcompat");
146 /***************************************************************/
148 #if defined(OSX) && !defined(PTY_DONE)
155 if ((f = open_controlling_pty(TtyName)) < 0)
163 /***************************************************************/
165 #if (defined(sequent) || defined(_SEQUENT_)) && !defined(PTY_DONE)
174 if ((f = getpseudotty(&s, &m)) < 0)
179 strncpy(PtyName, m, sizeof(PtyName));
180 strncpy(TtyName, s, sizeof(TtyName));
187 /***************************************************************/
189 #if defined(__sgi) && !defined(PTY_DONE)
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);
215 /***************************************************************/
217 #if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
226 strcpy(PtyName, "/dev/ptc");
227 if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
229 if (fstat(f, &buf) < 0)
234 sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
241 /***************************************************************/
243 #if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE)
251 int unlockpt __P((int)), grantpt __P((int));
252 #if defined(HAVE_GETPT) && defined(linux)
253 int getpt __P((void));
255 sigret_t (*sigcld)__P(SIGPROTOARG);
257 strcpy(PtyName, "/dev/ptmx");
258 #if defined(HAVE_GETPT) && defined(linux)
259 if ((f = getpt()) == -1)
261 if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
266 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
269 sigcld = signal(SIGCHLD, SIG_DFL);
270 if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
272 signal(SIGCHLD, sigcld);
276 signal(SIGCHLD, sigcld);
277 strncpy(TtyName, m, sizeof(TtyName));
284 /***************************************************************/
286 #if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
295 /* a dumb looking loop replaced by mycrofts code: */
296 strcpy (PtyName, "/dev/ptc");
297 if ((f = open (PtyName, O_RDWR | O_NOCTTY)) < 0)
299 strncpy(TtyName, ttyname(f), sizeof(TtyName));
300 if (eff_uid && access(TtyName, R_OK | W_OK))
314 /***************************************************************/
316 #if defined(HAVE_OPENPTY) && !defined(PTY_DONE)
323 if (openpty(&f, &s, TtyName, NULL, NULL) != 0)
333 /***************************************************************/
340 register char *p, *q, *l, *d;
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)
359 if (eff_uid && access(TtyName, R_OK | W_OK))
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
371 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
372 if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)